在virtualenv中使用鼻子的问题

Rya*_*yan 46 python nose virtualenv nosetests

我无法在virtualenv项目中使用nose(nosetests) - 它似乎无法找到virtualenv环境中安装的软件包.

奇怪的是我可以设置

test_suite = 'nose.collector'
Run Code Online (Sandbox Code Playgroud)

在setup.py中运行测试就好了

python setup.py test
Run Code Online (Sandbox Code Playgroud)

但是当直接运行nosetests时,会出现各种导入错误.

我尝试了系统范围的鼻子安装和virtualenv鼻子包,没有运气.

有什么想法吗?

谢谢!!

edw*_*ard 60

您需要在虚拟环境中安装一个nose副本.为了强制将nose安装到virtualenv中,即使它已经安装在全局站点包中,也要pip install使用-I标志运行:

(env1)$ pip install nose -I
Run Code Online (Sandbox Code Playgroud)

从那时起你可以nosetests像往常一样运行.

  • +1这对我有用.此外,正如Ceasar指出的那样,我还必须通过运行`deactivate`然后重新激活来刷新virtualenv. (19认同)
  • 似乎人们可能不得不刷新virtualenv.也就是说,`which nosetests`应该指向virtualenv中的可执行文件. (12认同)

Joh*_*kin 44

你能跑myenv/bin/python /usr/bin/nosetests吗?这应该使用虚拟环境的库集运行Nose.

  • 如果使用其他开发人员将使用的脚本,你可以做`python \`which nosetests \`` (3认同)
  • 试试这个别名nosetests ='/ usr/bin/env python $(which nosetests)' (3认同)

And*_*nca 10

在相同的情况下,我需要重新加载virtualenv路径才能正确更新:

deactivate
env/bin/activate
Run Code Online (Sandbox Code Playgroud)


npi*_*nto 9

我遇到了类似的问题.以下解决方法有助于:

python `which nosetests` 
Run Code Online (Sandbox Code Playgroud)

(而不仅仅是nosestests)


Pav*_*pin 8

这对我有用:

$ 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"
Run Code Online (Sandbox Code Playgroud)

我创建了一个非常基本的包slither,它具有setup.pytest_suite上面提到的相同的属性.然后我把包源放在下面env1/src.

如果你向里看env1/src,你会看到:

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 
Run Code Online (Sandbox Code Playgroud)

我可以使用test子命令运行测试:

(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
Run Code Online (Sandbox Code Playgroud)

或者,我可以运行相同的测试nosetests:

(env1)$ pushd src
(env1)$ nosetests slither/
..
Ran 2 tests in 0.007s
OK
(env1)$ popd
Run Code Online (Sandbox Code Playgroud)

另请注意,nosetests可执行文件可能很挑剔.--exe如果您希望它在可执行的python模块中发现测试,则可以传递.