在virtualenv中使用鼻子的问题

时间:2022-01-19 22:37:06

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项目中使用nose(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.

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

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

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

Any thoughts?

有什么想法吗?

Thanks!!

谢谢!!

8 个解决方案

#1


44  

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。

#2


58  

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:

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

(env1)$ pip install nose -I

From then on you can just run nosetests as usual.

从那时起你可以像往常一样运行测试。

#3


9  

I got a similar problem. The following workaround helped:

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

python `which nosetests` 

(instead of just nosestests)

(而不仅仅是nosestests)

#4


9  

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

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

deactivate
env/bin/activate

#5


8  

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.

我创建了一个非常基本的包滑动,它在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 test subcommand:

我可以使用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 nosetests can be picky about executables. You can pass --exe if you want it to discover tests in python modules that are executable.

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

#6


1  

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的答案会有同样的效果

#7


0  

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安装nosetests时,在.virtualenvs / / bin中安装了一个nosetests可执行文件,它(不出所料)与virtualenv一起正常运行。

#8


0  

You might have a nosetests that is installed somewhere else in your PATH with higher priority than the one installed in your virtualenv. A quick way to give the nose module and associated nosetests script installed in your current virtualenv top priority is to edit your PATH:

您可能在PATH中的其他位置安装了鼻子测试,其优先级高于您在virtualenv中安装的鼻子测试。在当前virtualenv中优先安装鼻子模块和相关鼻子测试脚本的快速方法是编辑PATH:

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

#1


44  

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。

#2


58  

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:

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

(env1)$ pip install nose -I

From then on you can just run nosetests as usual.

从那时起你可以像往常一样运行测试。

#3


9  

I got a similar problem. The following workaround helped:

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

python `which nosetests` 

(instead of just nosestests)

(而不仅仅是nosestests)

#4


9  

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

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

deactivate
env/bin/activate

#5


8  

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.

我创建了一个非常基本的包滑动,它在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 test subcommand:

我可以使用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 nosetests can be picky about executables. You can pass --exe if you want it to discover tests in python modules that are executable.

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

#6


1  

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的答案会有同样的效果

#7


0  

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安装nosetests时,在.virtualenvs / / bin中安装了一个nosetests可执行文件,它(不出所料)与virtualenv一起正常运行。

#8


0  

You might have a nosetests that is installed somewhere else in your PATH with higher priority than the one installed in your virtualenv. A quick way to give the nose module and associated nosetests script installed in your current virtualenv top priority is to edit your PATH:

您可能在PATH中的其他位置安装了鼻子测试,其优先级高于您在virtualenv中安装的鼻子测试。在当前virtualenv中优先安装鼻子模块和相关鼻子测试脚本的快速方法是编辑PATH:

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