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


当前回答

我最近也遇到了类似的问题。对我来说,它的工作方式是意识到“setup.py”是错误的

以前我删除了我以前的src文件夹,并添加了一个新的与其他名称,但我没有改变setup.py(新手错误我猜)。

因此,将setup.py指向正确的packages文件夹对我来说很有用

from setuptools import find_packages, setup

setup(
    name="-> YOUR SERVICE NAME <-",
    extras_Require=dict(test=["pytest"]),
    packages=find_packages(where="->CORRECT FOLDER<-"),
    package_dir={"": "->CORRECT FOLDER<-"},
)

另外,不要在test文件夹或根文件夹中使用init.py。

希望它能帮助到别人=)

最好!

其他回答

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

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

编辑你的conftest.py并添加以下代码行:

import os, sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(file), '..')))

如果试图通过终端运行测试用例,请使用以下ex:

python -m pytest test_some_step_file_steps.py --html=HTML_step_file_output.html --self-contained-html

在我的例子中,问题是文件名和类名完全相同:分别是FrameAnalyzer.py和FrameAnalyzer。一旦我将文件名更改为frame_analyzer.py,一切都正常了。

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

我的情况:

所有配置都是正确的,除了我忘记删除默认的my_app/tests.py 删除后,python -m pytest tests/my_app可以正常工作 pytest.ini:

[pytest]
DJANGO_SETTINGS_MODULE=necktie_hy.settings
python_files=*/tests/*/test_*.py
python_classes=Test