我正在用Python编写一个包。我使用virtualenv。我在virtualenv的.pth路径中设置了模块的根路径,这样我就可以在开发代码和测试时导入包的模块(问题1:这是一种好方法吗?)这很好(这是一个例子,这是我想要的行为):

(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python
Python 2.7.12 (default, Jul  1 2016, 15:12:24) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from rc import ns
>>> exit()
(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ python tests/test_ns.py 
issued command: echo hello
command output: hello

但是,如果我尝试使用PyTest,我会得到一些导入错误消息:

(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ pytest
=========================================== test session starts ============================================
platform linux2 -- Python 2.7.12, pytest-3.0.5, py-1.4.31, pluggy-0.4.0
rootdir: /home/zz/Desktop/GitFolders/rc, inifile: 
collected 0 items / 1 errors 

================================================== ERRORS ==================================================
________________________________ ERROR collecting tests/test_ns.py ________________________________
ImportError while importing test module '/home/zz/Desktop/GitFolders/rc/tests/test_ns.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_ns.py:2: in <module>
    from rc import ns
E   ImportError: cannot import name ns
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================= 1 error in 0.09 seconds ==========================================
(VEnvTestRc) zz@zz:~/Desktop/GitFolders/rc$ which pytest
/home/zz/Desktop/VirtualEnvs/VEnvTestRc/bin/pytest

我有点困惑,这似乎表明一个导入错误,但Python做得很好,为什么有一个问题,特别是与PyTest?对原因/补救(问题2)有什么建议吗?我在谷歌上搜索了PyTest的“ImportError: cannot import”错误,但我得到的点击率与丢失的python路径和补救措施有关,这似乎不是这里的问题。有什么建议吗?


当前回答

我不能说我理解为什么这样工作,但我有同样的问题,如果我运行python -m pytest,测试工作正常。

我在一个virtualenv中,pytest也在全局范围内可用:

(proj)tom@neon ~/dev/proj$ type -a python
python is /home/tom/.virtualenvs/proj/bin/python
python is /usr/bin/python

(proj)tom@neon ~/dev/proj$ python -V
Python 3.5.2

(proj)tom@neon ~/dev/proj$ type -a pytest
pytest is /home/tom/.virtualenvs/proj/bin/pytest
pytest is /usr/bin/pytest

(proj)tom@neon ~/dev/proj$ pytest --version
This is pytest version 3.5.0, imported from /home/tom/.virtualenvs/proj/lib/python3.5/site-packages/pytest.py

其他回答

这是一篇中篇文章!描述问题!

问题在于您使用的是哪个pytest以及您使用的是虚拟环境。 如果您在系统范围内(换句话说,在虚拟环境之外)安装了pytest,那么pytest有一个讨厌的习惯,就是只在虚拟环境之外寻找模块!如果您的项目使用的模块只安装在您的虚拟环境中,并且您正在使用系统范围的pytest,那么即使您激活了虚拟环境,它也不会找到该模块

以下是具体步骤

退出任何虚拟环境 使用Pip卸载pytest 激活项目的虚拟环境 在虚拟环境中安装pytest Pytest现在将找到仅虚拟环境的包!

可能是Pytest没有将包作为Python模块读取,而Python是(可能是由于路径问题)。尝试更改pytest脚本的目录或显式地将模块添加到PYTHONPATH中。

也可能是您的机器上安装了两个版本的Python。检查Python源代码中的pytest和您运行的Python shell。如果它们是不同的(即Python 2 vs 3),使用source activate来确保您运行的pytest安装在安装模块的同一个Python中。

在我的例子中,我在一个容器中工作,不幸的是,pytest倾向于使用python2.7而不是我选择的python3解释器。

在我的情况下,这是有效的:

python3 -m pytest

我的文件夹结构

/
app/
-module1.py
-module2.py
-tests/
--test_module1.py
--test_module2.py
requirements.txt
README.md

我只是通过删除项目根目录中的__init__.py来解决这个问题:

.
├── __init__.py <--- removed
├── models
│   ├── __init__.py
│   ├── address.py
│   ├── appointment.py
│   └── client.py
├── requirements.txt
├── setup.cfg
├── tests
│   ├── __init__.py
│   ├── models
│   │   ├── __init__.py
│   │   ├── appointment_test.py
│   │   └── client_test.py
│   └── other_test.py
└── script.py

对于那些尝试了所有方法仍然出错的人,我有一个解决方法。

进入pytest安装目录下的“pytest-env”文件夹。

打开pyvenv.cfg文件。

在文件中将include-system-site-packages从false更改为true。

home = /usr/bin
include-system-site-packages = true
version = 3.6.6

希望它能起作用。别忘了投票。