如何在 bash 脚本中使用 Python 3.6 解释器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45507488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Python 3.6 interpreter inside a bash script?
提问by Olga Zhukova
I want to have a little script, that will find, run and report about all the tests in the folder, like this one:
我想要一个小脚本,它将查找、运行和报告文件夹中的所有测试,如下所示:
#!/bin/bash
coverage run -m unittest discover
coverage report -m
But, when I run it, I get some errors, which I do not get on Windows (like using of super()
without an argument). As I've understood, it's connected with the fact, that build-in and default version of Python on Linux is 2.x, whereas I am using 3.6. How should I change the script, so it would use Python 3.6 interpreter?
但是,当我运行它时,我遇到了一些错误,这些错误在 Windows 上是super()
没有的(比如不带参数使用 of )。据我了解,这与以下事实有关,Linux 上 Python 的内置和默认版本是 2.x,而我使用的是 3.6。我应该如何更改脚本,以便它使用 Python 3.6 解释器?
EDIT: So here's one of the files with tests that I run:
编辑:所以这是我运行的测试文件之一:
#!/usr/bin/env python3
import unittest
import random
import math
import sort_functions as s
from comparison_functions import less, greater
class BaseTestCases:
class BaseTest(unittest.TestCase):
sort_func = None
def setUp(self):
self.array_one = [101, -12, 99, 3, 2, 1]
self.array_two = [random.random() for _ in range(100)]
self.array_three = [random.random() for _ in range(500)]
self.result_one = sorted(self.array_one)
self.result_two = sorted(self.array_two)
self.result_three = sorted(self.array_three)
def tearDown(self):
less.calls = 0
greater.calls = 0
def test_sort(self):
result_one = self.sort_func(self.array_one)
result_two = self.sort_func(self.array_two)
result_three = self.sort_func(self.array_three)
self.assertEqual(self.result_one, result_one)
self.assertEqual(self.result_two, result_two)
self.assertEqual(self.result_three, result_three)
# and some more tests here
class TestBubble(BaseTestCases.BaseTest):
def setUp(self):
self.sort_func = s.bubble_sort
super().setUp()
# and some more classes looking like this
And the error:
和错误:
ERROR: test_key (test_sort_func.TestBubble)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/lelik/Desktop/Sorters/test_sort_func.py", line 67, in setUp
super().setUp()
TypeError: super() takes at least 1 argument (0 given)
回答by Laszlowaty
First, install it for your python3 (if you have it and pip
installed)
sudo python3 -m pip install coverage
首先,为你的 python3 安装它(如果你已经pip
安装了它)
sudo python3 -m pip install coverage
Then, in order to run coverage for python3, run python3 -m coverage report -m
然后,为了运行 python3 的覆盖率,运行 python3 -m coverage report -m
So your final script should look like this:
所以你的最终脚本应该是这样的:
#!/bin/bash
python3 -m coverage run -m unittest discover
python3 -m coverage report -m
Also you can replace python3
with path to your pythons bin. For example /usr/bin/python3
. So You can call it this way as well:
你也可以用python3
你的pythons bin的路径替换。例如/usr/bin/python3
。所以你也可以这样称呼它:
#!/bin/bash
/usr/bin/python3 -m coverage run -m unittest discover
/usr/bin/python3 -m coverage report -m
回答by larsks
The problem is that the coverage
command on your Linux host has been installed for Python 2. That is, somewhere there exists a coverage
script that starts with:
问题是coverage
你的 Linux 主机上的命令已经为 Python 2 安装。也就是说,某处存在一个coverage
以以下开头的脚本:
#!/usr/bin/python
And on your system, /usr/bin/python
is python 2.
在你的系统上,/usr/bin/python
是 python 2。
The best solution here is probably to set up a Python 3 virtual environmentfor running your tests (and then installing coverage
into that virtualenv). You may also want to investigate tox, which will handle this for you automatically.
这里最好的解决方案可能是设置一个 Python 3虚拟环境来运行你的测试(然后安装coverage
到那个 virtualenv 中)。您可能还想调查tox,它会自动为您处理。