首页 文章

python无法导入模块

提问于
浏览
30

我正在使用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:无法导入'错误,但是我得到的命中与缺少python路径和补救相关,这似乎不是问题 . 有什么建议?

10 回答

  • 38

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

    或者可能是您的计算机上安装了两个版本的Python . 检查你的Python源代码是否有pytest以及你运行的python shell . 如果它们不同(即Python 2对3),请使用 source activate 以确保您正在为安装该模块的同一个python运行pytest .

  • 3

    在我的情况下,导致导入错误,因为包指向另一个包含 same name 的包/目录,其路径比我实际想要的文件夹高一级 . 我想这也解释了为什么有些人需要删除_ init _.py而其他人需要添加回来 .

    我只是在 python 控制台和 pytest 脚本中放置 print(the_root_package.__path__) (在 import the_root_package 之后)来比较 difference

    BOTTOM LINE:当您执行 python 时,导入的包可能与运行 pytest 时的包不同 .

  • 2

    对于那些尝试过所有并且仍然出错的人,我有一个解决方法 .

    folder where pytest is installed 中,转到 pytest-env 文件夹 .

    打开 pyvenv.cfg 文件 .

    在文件中将 include-system-site-packagesfalse 更改为 true .

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

    希望它有效 . 别忘了投票 .

  • 0

    我遇到了同样的问题,但出于另一个原因而不是提到的问题:

    我在全局安装了py.test,而软件包安装在虚拟环境中 .

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

  • 0

    将软件包安装到您的虚拟环境中 .
    然后启动一个新shell并再次获取您的虚拟环境 .

  • 11

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

    sudo pip install ./ --upgrade

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

  • 3

    我不能说我理解为什么会这样,但我有同样的问题,如果我运行 python -m pytest 测试工作正常 .

    我是一个虚拟的人,全球都有pytest:

    (proj)tom@neon ~/dev/proj$ type -a python
    python is /home/tom/.virtualenvs/proj/bin/python
    python is /usr/bin/python
    
    (proj)tom@neon ~/dev/proj$ python -V
    Python 3.5.2
    
    (proj)tom@neon ~/dev/proj$ type -a pytest
    pytest is /home/tom/.virtualenvs/proj/bin/pytest
    pytest is /usr/bin/pytest
    
    (proj)tom@neon ~/dev/proj$ pytest --version
    This is pytest version 3.5.0, imported from /home/tom/.virtualenvs/proj/lib/python3.5/site-packages/pytest.py
    
  • 1

    如果您有 tests.py 文件和带有 tests/__init__.py 的tests文件夹,则会发生此问题 .

    在收集pytest期间找到文件夹,但是当它尝试从文件夹导入测试文件时, tests.py 文件将导致导入问题 .

    要修复,只需删除 tests.py 文件并将所有测试放在 tests/ 文件夹中 .

    对于您的具体情况,修复将是:

    • 删除文件 /home/zz/Desktop/GitFolders/rc/tests.py

    • 确保 /home/zz/Desktop/GitFolders/rc/tests/__init__.py 存在

  • 0

    我刚刚通过删除项目根目录中的__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
    
  • 7

    找到答案:

    如果您打算使用pytest,请勿将 __init__.py 文件放在包含TESTS的文件夹中 . 我有一个这样的文件,删除它解决了问题 .

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

相关问题