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


当前回答

保持一切不变,只是在根文件夹中添加了一个空白的测试文件。解决了这个问题

以下是调查结果,这个问题确实困扰了我一段时间。 文件夹结构是

mathapp/
    - server.py  
    - configuration.py 
    - __init__.py 
    - static/ 
       - home.html  
tests/            
    - functional 
       - test_errors.py 
    - unit  
       - test_add.py

和pytest会抱怨ModuleNotFoundError,并给出提示:

确保你的测试模块/包有有效的Python名称。

我介绍了一个与mathsapp和tests目录相同级别的模拟测试文件。文件里什么也没有。现在pytest没有抱怨了。

不包含文件的结果

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\mak2006\workspace\0github\python-rest-app-cont
collected 1 item / 1 error

=================================== ERRORS ====================================
_______________ ERROR collecting tests/functional/test_func.py ________________
ImportError while importing test module 'C:\mainak\workspace\0github\python-rest-app-cont\tests\functional\test_func.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\functional\test_func.py:4: in <module>
    from mathapp.service import sum
E   ModuleNotFoundError: No module named 'mathapp'
=========================== short test summary info ===========================
ERROR tests/functional/test_func.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.24s ===============================

文件的结果

$ pytest
============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-5.4.2, py-1.8.1, pluggy-0.13.1
rootdir: C:\mak2006\workspace\0github\python-rest-app-cont
collected 2 items

tests\functional\test_func.py .                                          [ 50%]
tests\unit\test_unit.py .                                                [100%]

============================== 2 passed in 0.11s ==============================

其他回答

从pytest 7.0开始,你现在可以在pytest.ini中添加pythonpath。不需要在根目录中添加__init__.py或conftest.py。

[pytest]
minversion = 7.0
addopts = --cov=src
pythonpath = src
testpaths =
    tests

可以不带任何参数运行pytest。

https://docs.pytest.org/en/7.0.x/reference/reference.html#confval-pythonpath

Python导入系统的又一次巨大胜利。我认为没有达成共识的原因是,什么可行可能取决于您的环境和您在其之上使用的工具。

我从VS Code中使用这个,在conda环境下的Windows测试资源管理器中,Python 3.8。

我要工作的设置是:

mypkg/
    __init__.py
    app.py
    view.py
tests/
    test_app.py
    test_view.py

在这种设置下,智能感知和测试发现都起作用。

请注意,我最初尝试了以下建议。

src/
    mypkg/
        __init__.py
        app.py
        view.py
tests/
    test_app.py
    test_view.py

我找不到从VS Code得到这个工作的方法,因为src文件夹只是吹了导入系统的头脑。我可以想象,有一种方法可以让它从命令行工作。作为一个相对较新的Python编程转换者,它给了我一种使用COM的怀旧感觉,但没有那么有趣。

当我试图在我的测试文件中导入一个新类时,我遇到了相同/类似的问题。在我的例子中,我将新导入移动到测试文件中的最后一个导入,然后问题就消失了。但不完全确定原因。

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

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

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

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

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