Problems using nose in a virtualenv

14,275

Solution 1

Are you able to run myenv/bin/python /usr/bin/nosetests? That should run Nose using the virtual environment's library set.

Solution 2

You need to have a copy of nose installed in the virtual environment. In order to force installation of nose into the virtualenv, even though it is already installed in the global site-packages, run pip install with the -I flag:

(env1)$ pip install nose -I

From then on you can just run nosetests as usual.

Solution 3

In the same situation I needed to reload the virtualenv for the path to be correctly updated:

deactivate
env/bin/activate

Solution 4

I got a similar problem. The following workaround helped:

python `which nosetests` 

(instead of just nosestests)

Solution 5

Here's what works for me:

$ virtualenv --no-site-packages env1
$ cd env1
$ source bin/activate            # makes "env1" environment active,
                                 # you will notice that the command prompt
                                 # now has the environment name in it.

(env1)$ easy_install nose        # install nose package into "env1"

I created a really basic package slither that had, in its setup.py, same test_suite attribute as you mentioned above. Then I placed the package source under env1/src.

If you looked inside env1/src, you'd see:

slither/setup.py
slither/slither/__init__.py
slither/slither/impl.py          # has some very silly code to be tested
slither/slither/tests.py         # has test-cases 

I can run the tests using test subcommand:

(env1)$ pushd src/slither
(env1)$ python setup.py test
# ... output elided ...
test_ctor (slither.tests.SnakeTests) ... ok
test_division_by_zero (slither.tests.SnakeTests) ... ok
Ran 2 tests in 0.009s
OK
(env1)$ popd

Or, I can run the same tests with nosetests:

(env1)$ pushd src
(env1)$ nosetests slither/
..
Ran 2 tests in 0.007s
OK
(env1)$ popd

Also note that nosetests can be picky about executables. You can pass --exe if you want it to discover tests in python modules that are executable.

Share:
14,275

Related videos on Youtube

Ryan
Author by

Ryan

I am an associate professor of Anthropology at Lehman College and The Graduate School of the City University of New York. My research projects are directed towards understanding the processes that created the current patterns of human and primate genetic diversity.

Updated on January 17, 2021

Comments

  • Ryan
    Ryan over 3 years

    I am unable to use nose (nosetests) in a virtualenv project - it can't seem to find the packages installed in the virtualenv environment.

    The odd thing is that i can set

    test_suite = 'nose.collector'
    

    in setup.py and run the tests just fine as

    python setup.py test
    

    but when running nosetests straight, there are all sorts of import errors.

    I've tried it with both a system-wide installation of nose and a virtualenv nose package and no luck.

    Any thoughts?

    Thanks!!

    • Ryan
      Ryan about 15 years
      On a related note, nose works fine "out of the box" on a different computer... so there's something wonky with the system I was having problems on.
  • bcoughlan
    bcoughlan about 11 years
    If using with scripts that other developers will use, you can do python `which nosetests`
  • Ceasar Bautista
    Ceasar Bautista almost 11 years
    It seems that one might have to refresh the virtualenv. Namely, which nosetests should point to an executable inside of the virtualenv.
  • Tom
    Tom over 10 years
    +1 This is what worked for me. Also, as Ceasar points out, I also had to refresh the virtualenv by running deactivate and then reactivating.
  • Necrolyte2
    Necrolyte2 over 10 years
    Try this alias nosetests='/usr/bin/env python $(which nosetests)'
  • David Gay
    David Gay over 9 years
    After you use this command, then deactivate, then workon ENV, it seems you can then use python `which nosetests` .
  • Joost
    Joost over 8 years
    When which nosetests does not correctly point to the venv, check if you have installed nose or nose2, and note that nose2 does not use the nosetests executable (but uses nose2 instead). D-oh!
  • hansmosh
    hansmosh over 8 years
    This is the one that did it for me. Any idea why you need to re-activate after installing nose (or nose2, in my case)?
  • Andrea Zonca
    Andrea Zonca over 8 years
    @hansmosh, cannot test right now, it would be useful to check all the paths
  • Steven Stip
    Steven Stip over 5 years
    I had nose installed globally so the nosetests executable didn't resolve properly. the following worked: python which nosetests but this is the actual solution for me.