我正在用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路径和补救措施有关,这似乎不是这里的问题。有什么建议吗?


当前回答

对我来说,这个问题是我的测试文件命名为:xxx.test.py

我将文件命名为:xxx_test.py

但是我想命名为test_xxx.py也可以。

其他回答

我有一个类似的问题,完全相同的错误,但不同的原因。我运行的测试代码很好,但是针对的是模块的旧版本。在我代码的前一个版本中,一个类存在,而另一个不存在。在更新代码之后,我应该运行以下命令来安装它。

Sudo PIP install ./——upgrade

在安装更新的模块时,运行pytest产生了正确的结果(因为我使用了正确的代码库)。

请查看这里:https://docs.pytest.org/en/documentation-restructure/background/pythonpath.html

我有一个问题与pytest(已解决使用python -m pytest);错误在于

FileNotFoundError: [Errno 2] No such file or directory: '/usr/local/lib/python3.9/site-packages/...

我发现问题是在tests/和tests/子文件夹中缺少__init__.py。

在我的例子中,发生导入错误是因为包指向具有相同名称的另一个包/目录,其路径比我实际需要的文件夹高一级。 我认为这也解释了为什么有些人需要删除_ init _.py,而另一些人需要添加回来。

我只是把print(the_root_package.__path__)(在导入the_root_package之后)放在python控制台和pytest脚本中来比较差异

底线:当您使用python时,您导入的包可能与运行pytest时的包不同。

[解析] 在直接进入删除/添加__init__.py的解决方案之前,我们可能还想看看在你的类中是如何导入的。实际上,我花了一天的时间在__init__.py上,认为这可能是问题所在:)然而,这是相当有意义的。

在我的例子中,从一个python类调用类到另一个python类的方式是错误的,这抛出了ImportError。修正了类/模块被调用的方式,它像魅力一样工作。希望,这也能帮助其他人。

是的,对于类似的错误,我们可能有不同的解决方案,这取决于代码的编写方式。最好在自我调试上花更多的时间。经验教训:)快乐的编码!!

我不能说我理解为什么这样工作,但我有同样的问题,如果我运行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