如何让nosetests使用python3
我尝试使用 nosetests
❯ nosetests '/pathTo/test'
但是它却用 python 2.7
来运行我的测试:
sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)
所以有些测试失败了,因为它们是用 python 3.3
写的。
我想了个办法,安装了虚拟环境:
pyvenv-3.3 py3env
激活了它:
source ~/py3env/bin/activate
在虚拟环境中检查 Python 版本:
❯ python --version ⏎
Python 3.3.3
(py3env)
好的。
但是即使在虚拟环境中,nosetest 还是用 python 2.7
:
sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)
所以我的测试失败了。 怎么才能让 nose 使用 python3 呢?
4 个回答
这不是虚拟环境的问题,而是一个Linux系统的问题。
这意味着,当你在终端里输入命令nosetests
时,Linux会在它能找到的路径里(比如/bin
、/sbin
,或者你自己设置的其他路径)寻找这个可执行文件。
你的全局Python 2的nosetests
会被优先找到并执行。
而你的虚拟环境中的Python 3的nosetests
在可用路径列表中排得较后,因此根本没有被执行到。
我建议你在每个虚拟环境中只安装nose或者其他Python命令。
我找到了一种方法,可以在没有环境的情况下用 Python3 运行 nosetests:
cd /Library/Frameworks/Python.framework/Versions/3.3/bin
然后:
❯ nosetests-3.3 '/folder/with/tests'
nosetests-3.3
是用 python 3
来运行的。
就这么简单。
如果你想用 nosetests
这个命令,而不是 nosetests-3.3
,可以在 ~/.bash_profile
文件里添加:
nosetests()
{
/Library/Frameworks/Python.framework/Versions/3.3/bin/nosetests-3.3 $1
}
现在你可以从任何目录使用:
nosetests '/folder/with/tests'
它会使用 python3
。
安装的方法是:
sudo apt-get install python-nose python3-nose
运行的方法是:
nosetests-2.7 ; nosetests3
这样可以在Python 2和Python 3的环境下都运行测试。
在 Python 3.4
及更高版本中:为了让 nose 使用 python3
,只需要运行 ...
python3 -m "nose"
... 在包含测试的目标目录中。
不需要进行环境设置。