How to make nosetests use python3

Question:

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?

Asked By: Maxim Yefremov

||

Answers:

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.

Answered By: Maxim Yefremov

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.

Answered By: Maxim Yefremov

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.

Answered By: ArekBulski

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.

Answered By: l__flex__l
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.