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


当前回答

我得到这个使用VSCode。我有一个conda环境。我不认为VScode python扩展可以看到我正在做的更新。

python c:\Users\brig\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\testing_tools\run_adapter.py discover pytest -- -s --cache-clear test
Test Discovery failed:

我必须运行pip install ./——upgrade

其他回答

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

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

我不同意那些说你必须删除任何__init__.py文件的帖子。相反,您必须更改sys.path。

做个实验,打印sys。路径时正常运行的代码。 然后输出sys。路径,同时通过pytest运行代码。我想你会发现这两条路径之间有区别,这就是为什么pytest会中断。

为了解决这个问题,在第二个实验的第0个索引处插入第一个实验的路径。

让'/usr/exampleUser/Documents/foo'作为实验1的print(sys.path)的第一个元素。

下面是代码,应该解决你的问题:

导入系统 sys。path[0] = '/usr/exampleUser/Documents/foo'

把它放在文件的顶部,在实际的import语句之前。

来源:我自己也在处理这个问题,上面的过程解决了这个问题。

找到了答案:

如果你计划使用pytest,不要将__init__.py文件放在包含TESTS的文件夹中。我有一个这样的文件,删除它就解决了问题。

这实际上被埋在pytest 'ImportError: No module named YadaYadaYada'的PATH问题的第二个答案的评论中,所以我没有看到它,希望它在这里得到更多的可见性。

我的情况:

所有配置都是正确的,除了我忘记删除默认的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

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

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

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