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


当前回答

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

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

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

其他回答

请查看这里: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。

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

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

打开pyvenv.cfg文件。

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

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

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

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

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

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

我已将所有测试放在测试文件夹中,并得到相同的错误。我通过在该文件夹中添加__init__.py来解决这个问题,如下所示:

.
|-- Pipfile
|-- Pipfile.lock
|-- README.md
|-- api
|-- app.py
|-- config.py
|-- migrations
|-- pull_request_template.md
|-- settings.py
`-- tests
    |-- __init__.py <------
    |-- conftest.py
    `-- test_sample.py

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

Sudo PIP install ./——upgrade

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