python - ImportError 出现在 py.test 中,但在运行应用程序时不会出现

标签 python python-3.x importerror pytest circular-dependency

不像这个问题: Importing modules from a sibling directory for use with py.test 我可以从我的应用程序导入一些东西,但是从'inside' myapp 引发导入错误(看起来像循环依赖)在运行测试时而不是在运行 myapp 时独自一人:

$ python3 myapp/myapp.py
Some dummy string (correct output)

但是:

$ python3 -m pytest                                                                               
================================================================= test session starts =================================================================
platform linux -- Python 3.4.3, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/nico/temp/projects_structures/test04/myapp, inifile: 
plugins: cov-2.2.1
collected 0 items / 1 errors 

======================================================================= ERRORS ========================================================================
________________________________________________________ ERROR collecting tests/test_things.py ________________________________________________________
tests/test_things.py:4: in <module>
    from myapp.lib.core.base import do_things
myapp/lib/core/base.py:1: in <module>
    from lib import something
E   ImportError: No module named 'lib'
=============================================================== 1 error in 0.05 seconds ===============================================================

如您所见,问题不是 import来自测试文件的语句。它是从“内部”升起的 myapp .

这是完整的结构:

.
└── myapp
    ├── myapp
    │   ├── __init__.py
    │   ├── lib
    │   │   ├── core
    │   │   │   ├── base.py
    │   │   │   └── __init__.py
    │   │   └── __init__.py
    │   └── myapp.py
    └── tests
        └── test_things.py

myapp.py 包含:

#!/usr/bin/env python3

from lib.core import base

base.do_things()

lib/__init__.py 包含:

something = "Some dummy string (correct output)"

base.py 包含:

from lib import something

def do_things():
    print(something)
    return True

并且 test_things 包含:

import unittest
import sys
sys.path.insert(0, '..')
from myapp.lib.core.base import do_things

class DoThingsTestCase(unittest.TestCase):

    def test_do_things(self):
        self.assertTrue(do_things())

if __name__ == '__main__':
    unittest.main()

$PYTHONPATH似乎设置正确(所以这个: Py.test No module named * 没有回答我的问题)。 (或者,如果这不正确,我该如何更正?)

$ echo $PYTHONPATH
/home/nico/temp/projects_structures/test04/myapp/myapp

最佳答案

设置 PYTHONPATH 应该可以解决问题。

$ export PYTHONPATH=<ABSOLUTE PATH TO TOPMOST myapp dir>

(在本例中,它是 myapp/myapp 的路径;并确保您已导出 PYTHONPATH,而不仅仅是设置它)。

然后从 myapp 运行

$ py.test

$ python3 -m pytest

关于python - ImportError 出现在 py.test 中,但在运行应用程序时不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37560196/

相关文章:

Python OpenCV imshow() 函数未实现

python - pandas - 组合行字符串

python - 即使使用当前 python 版本安装 Numpy 导入错误

Python通过线程tkinter进行通信

python-3.x - ImportError:没有名为 '_tkinter' 的模块,请安装 python3-tk 包

python - Scipy - 稀疏库导入错误 : DLL load failed: %1 is not a valid Win32 application

python - 谷歌数据泄露防护 : "ValueError: Protocol message Value has no "stringValue"field.“

python - 如何从 python 获取 C++ 结构?

python-3.x - Python 中 CPU 使用情况的拆分结果

python - 导入错误:无法导入名称 'x'