在 virtualenv 中使用鼻子的问题

发布于 2024-07-19 04:46:18 字数 338 浏览 15 评论 0原文

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

奇怪的是,我可以

test_suite = 'nose.collector'

在 setup.py 中设置并运行测试,

python setup.py test

但当直接运行鼻子测试时,会出现各种导入错误。

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

有什么想法吗?

谢谢!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

逆流 2024-07-26 04:46:18

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

(env1)$ pip install nose -I

从那时起您可以照常运行nosetests

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.

薯片软お妹 2024-07-26 04:46:18

您可以运行 myenv/bin/python /usr/bin/nosetests 吗? 这应该使用虚拟环境的库集运行 Nose。

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

酒浓于脸红 2024-07-26 04:46:18

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

deactivate
env/bin/activate

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

deactivate
env/bin/activate
故人如初 2024-07-26 04:46:18

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

python `which nosetests` 

而不仅仅是 nosestests

I got a similar problem. The following workaround helped:

python `which nosetests` 

(instead of just nosestests)

回心转意 2024-07-26 04:46:18

这对我有用:

$ 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"

我创建了一个非常基本的包 slither,在其 setup.py 中,具有与您上面提到的相同的 test_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 

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

或者,我可以使用 nosetests< 运行相同的测试/code>:

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

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

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.

半世蒼涼 2024-07-26 04:46:18

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

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

请你别敷衍 2024-07-26 04:46:18

也许这是最近的变化,但对我来说,当我通过 pip 安装osetests 时,.virtualenvs//bin 中安装了一个osetests 可执行文件,它(毫不奇怪)可以正确运行虚拟环境。

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.

烟酉 2024-07-26 04:46:18

您可能在 PATH 中的其他位置安装了 nosetests,其优先级高于安装在 virtualenv 中的优先级。 为当前 virtualenv 中安装的 nose 模块和关联的 nosetests 脚本提供最高优先级的快速方法是编辑您的 PATH

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

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:

export PATH=/path/to/current/virtualenv/bin:$PATH
紫轩蝶泪 2024-07-26 04:46:18

聚会迟到了。

无论如何,如果你在 2021 年看到这种情况......
对我来说,围绕nose、nose-cov 和 pinocchio 的这些问题已通过将我的虚拟环境更新到至少 Python 3.8.2 来解决。

Late to the party.

Regardless, if you see this in the year 2021...
These issues, for me, surrounding nose, nose-cov and pinocchio were resolved by updating my Virtual Envs to at least Python 3.8.2..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文