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


当前回答

如果你在终端上运行Pytest:

使用——import-mode=append命令行标志运行pytest。

官方文档中的参数说明:https://docs.pytest.org/en/stable/pythonpath.html


乌利希期刊指南: 之前我也写过如果你使用PyCharm如何做同样的事情,但是社区不喜欢扩展答案,所以我删除了可能对有类似问题的人有帮助的额外信息。

其他回答

另一种特殊情况:

我用毒理剂有问题。所以我的程序运行良好,但通过tox的单元测试一直抱怨。 在安装包(程序所需)之后,您需要在tox.ini中额外指定单元测试中使用的包

[testenv]
deps =
    package1
    package2 
...

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

我通常建议使用绝对路径,并从根文件夹或父文件夹运行应用程序和/或测试。

如果有人在Uvicorn的FastAPI中收到这个错误,只需运行例如。

uvicorn app.main:app --host 0.0.0.0 --port 8080

如果你的文件夹中需要一个init.py文件,请复制该文件夹并删除其中的init.py来运行你的测试,它适用于本地项目。如果你需要定期运行测试,看看是否可以将init.py移到一个单独的文件中。

如果你在终端上运行Pytest:

使用——import-mode=append命令行标志运行pytest。

官方文档中的参数说明:https://docs.pytest.org/en/stable/pythonpath.html


乌利希期刊指南: 之前我也写过如果你使用PyCharm如何做同样的事情,但是社区不喜欢扩展答案,所以我删除了可能对有类似问题的人有帮助的额外信息。