python 在 virtualenv 中使用鼻子的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/864956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 21:00:35  来源:igfitidea点击:

Problems using nose in a virtualenv

pythonvirtualenvnosenosetests

提问by Ryan

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

我无法在 virtualenv 项目中使用鼻子(nosetests) - 它似乎无法找到安装在 virtualenv 环境中的包。

The odd thing is that i can set

奇怪的是我可以设置

test_suite = 'nose.collector'

in setup.py and run the tests just fine as

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

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.

我已经尝试过在系统范围内安装鼻子和 virtualenv 鼻子包,但没有运气。

Any thoughts?

有什么想法吗?

Thanks!!

谢谢!!

采纳答案by John Millikin

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

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

回答by edward

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 installwith the -Iflag:

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

(env1)$ pip install nose -I

From then on you can just run nosetestsas usual.

从那时起,您就可以nosetests照常运行了。

回答by Andrea Zonca

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

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

deactivate
env/bin/activate

回答by npinto

I got a similar problem. The following workaround helped:

我遇到了类似的问题。以下解决方法有所帮助:

python `which nosetests` 

(instead of just nosestests)

(而不仅仅是nosestests

回答by Pavel Repin

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 slitherthat had, in its setup.py, same test_suiteattribute as you mentioned above. Then I placed the package source under env1/src.

我创建了一个非常基本的包slither,它的属性与您上面提到的setup.py相同test_suite。然后我将包源放在env1/src.

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

如果你往里面看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 

I can run the tests using testsubcommand:

我可以使用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

Or, I can run the same tests with nosetests:

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

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

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

另请注意,nosetests对可执行文件可能很挑剔。--exe如果您希望它发现可执行的 Python 模块中的测试,则可以通过。

回答by eggonlegs

If all else fails, try installing nose in your venv, and/or run nosetests-2.7. I believe @andrea-zonca's answer has the same effect if your venv python is 2.7

如果所有其他方法都失败了,请尝试在您的 venv 中安装鼻子,和/或运行nosetests-2.7. 如果您的 venv python 是 2.7,我相信@andrea-zonca 的答案具有相同的效果

回答by Josh Bleecher Snyder

Perhaps this is a recent change, but for me, when I installed nosetests through pip, there was a nosetests executable installed in .virtualenvs/<env>/bin, which (unsurprisingly) operates correctly with the virtualenv.

也许这是最近的变化,但对我来说,当我通过 pip 安装鼻子测试时,在 中安装了一个鼻子测试可执行文件.virtualenvs/<env>/bin,它(不出所料)与 virtualenv 一起正常运行。

回答by orluke

You might have a noseteststhat is installed somewhere else in your PATHwith higher priority than the one installed in your virtualenv. A quick way to give the nosemodule and associated nosetestsscript installed in your current virtualenv top priority is to edit your PATH:

您可能有一个nosetests安装在您的其他地方的PATH优先级高于安装在您的 virtualenv 中的那个。为当前 virtualenv 中安装的nose模块和相关nosetests脚本提供最高优先级的快速方法是编辑您的PATH

export PATH=/path/to/current/virtualenv/bin:$PATH