[英]ImportError shows up with py.test, but not when running the app
Unlike in this question: Importing modules from a sibling directory for use with py.test I can import something from my app, but there's an import error (looking like a circular dependency) that raises from 'inside' myapp
while running the test and not when running myapp
alone: 与这个问题不同: 从兄弟目录导入模块以便与py.test一起使用我可以从我的应用程序中导入一些东西,但是在运行测试时从内部'
myapp
'引发导入错误(看起来像循环依赖)而不是单独运行myapp
时:
$ python3 myapp/myapp.py
Some dummy string (correct output)
But: 但:
$ 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 ===============================================================
As you can see, the problem is not the import
statement from the test file. 如您所见,问题不在于测试文件中的
import
语句。 It's raised from 'inside' myapp
. 它是从'内部'
myapp
提升的。
Here is the complete structure: 这是完整的结构:
.
└── myapp
├── myapp
│ ├── __init__.py
│ ├── lib
│ │ ├── core
│ │ │ ├── base.py
│ │ │ └── __init__.py
│ │ └── __init__.py
│ └── myapp.py
└── tests
└── test_things.py
myapp.py contains: myapp.py包含:
#!/usr/bin/env python3
from lib.core import base
base.do_things()
lib/__init__.py contains: lib / __ init__.py包含:
something = "Some dummy string (correct output)"
base.py contains: base.py包含:
from lib import something
def do_things():
print(something)
return True
and test_things contains: 和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()
And $PYTHONPATH
seems correctly set (so this: Py.test No module named * doesn't answer my problem). 并且
$PYTHONPATH
似乎正确设置(所以这个: Py.test没有名为*的模块没有回答我的问题)。 (Or if this is not correct, how can I correct it?) (或者,如果这不正确,我该如何纠正?)
$ echo $PYTHONPATH
/home/nico/temp/projects_structures/test04/myapp/myapp
Setting the PYTHONPATH should do the trick. 设置PYTHONPATH应该可以解决问题。
$ export PYTHONPATH=<ABSOLUTE PATH TO TOPMOST myapp dir>
(in this example, it is the path to myapp/myapp; and ensure you've exported PYTHONPATH
, not only set it). (在这个例子中,它是myapp / myapp的路径;并确保您已导出
PYTHONPATH
,而不仅仅是设置它)。
and from myapp run either 并从myapp运行
$ py.test
or 要么
$ python3 -m pytest
Another way of doing this is to put a conftest.py
file into the top level of myapp
, or in myapp/tests
. 另一种方法是将
conftest.py
文件放入myapp
的顶级或myapp/tests
。 Like this: 像这样:
$ pwd
/home/nico/temp/projects_structures/test04/myapp/tests
$ touch conftest.py
$ cd ..
$ py.test
================================================================= test session starts =================================================================
platform linux2 -- Python 2.7.6, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/nico/temp/projects_structures/test04/myapp, inifile:
collected 1 items
tests/test_things.py .
============================================================== 1 passed in 0.05 seconds ===============================================================
$
(and PYTHONPATH
is empty: (和
PYTHONPATH
是空的:
$ echo $PYTHONPATH
$
) )
This way, py.test
automatically adds myapp
to PYTHONPATH
. 这样,
py.test
自动将myapp
添加到PYTHONPATH
。 This avoids to forget exporting PYTHONPATH
and will make the tests of myapp
easier for other devs (who neither will need to solve this problem). 这可以避免忘记导出
PYTHONPATH
并使其他开发人员(他们都不需要解决此问题)更容易进行myapp
测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.