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


当前回答

我今天遇到了这个问题,并通过从我的项目目录的根调用python -m pytest来解决它。

从同一位置调用pytest仍然会导致问题。

我的项目总监组织如下:

api/
 - server/
  - tests/
      - test_routes.py
  - routes/
      - routes.py
 - app.py

在我的test_routes.py中引入了模块路由,如下

希望有帮助!

其他回答

更新PYTHONPATH直到src文件夹

export PYTHONPATH=/tmp/pycharm_project_968/src

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的怀旧感觉,但没有那么有趣。

我只是通过删除项目根目录中的__init__.py来解决这个问题:

.
├── __init__.py <--- removed
├── models
│   ├── __init__.py
│   ├── address.py
│   ├── appointment.py
│   └── client.py
├── requirements.txt
├── setup.cfg
├── tests
│   ├── __init__.py
│   ├── models
│   │   ├── __init__.py
│   │   ├── appointment_test.py
│   │   └── client_test.py
│   └── other_test.py
└── script.py

将包安装到虚拟环境中。 然后启动一个新的shell并再次源您的虚拟环境。

我也遇到了同样的问题,但原因不是上面提到的那些:

py。测试是全局安装的,而包是安装在虚拟环境中的。

解决方案是在虚拟环境中安装pytest。(如果你的shell像Bash一样散列可执行文件,使用hash -r,或者使用py.test的完整路径)