How to make nosetests use python3

折月煮酒 提交于 2019-12-21 03:11:04

问题


I try to use nosetests
❯ nosetests '/pathTo/test'

but it uses python 2.7 for my tests:

sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)

So some of them fails, because they were written in python 3.3.

I work it around and installed virtual environment:

pyvenv-3.3 py3env

Activated it:

source ~/py3env/bin/activate

Check python virsion in virtual environment:

❯ python --version                                                                                 ⏎
Python 3.3.3
(py3env)

Ok. But nosetest still uses python2.7 even in virtual environment:

sys.version_info(major=2, minor=7, micro=5, releaselevel='final', serial=0)

So my tests fails. How to make nose use python3?


回答1:


In Python 3.4 and higher versions: in order to make nose use python3 just run ...

python3 -m "nose"

... in the target directory with the tests.

The environment setups are not required.




回答2:


To install:

sudo apt-get install python-nose python3-nose

To run:

nosetests-2.7 ; nosetests3

This runs the test suite under both PY2 and PY3.




回答3:


I found way to use nosetests with python3 without environment:

cd /Library/Frameworks/Python.framework/Versions/3.3/bin  

And then:

❯ nosetests-3.3 '/folder/with/tests'

nosetests-3.3 uses python 3

That's it.

And if your want use command nosetests instead of nosetests-3.3, add in ~/.bash_profile:

nosetests()
{
    /Library/Frameworks/Python.framework/Versions/3.3/bin/nosetests-3.3 $1
}

Now you can use:

nosetests '/folder/with/tests'

from any directory. It uses python3.




回答4:


This isn't a virtualenv issue as much as a linux issue.

This means that when you use the command nosetests from the terminal, linux looks within it's available paths (/bin, /sbin, or whatever it is by you) for such an executable file.

Your global python 2 nosetests is found first and executed.

Your virtualenv python3 nosetests is later in the available path list and therefore never reached.

I would suggest to only install nose or any other python command per virtual environment.



来源:https://stackoverflow.com/questions/22779289/how-to-make-nosetests-use-python3

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!