我有一个包含以下结构的包:
mypackage
|__ __init__.py
|
|__ dira
| |__ __init__.py
| |__ classa.py
|
|__ tests
|__ __init__.py
|__ test_classa.py
在test_classa.py
中,我使用语句import dira
。如果我从目录pytest tests
运行mypackage
命令,则会收到以下错误:
(myenv) D:\work\dev\mypackage>pytest tests
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.5, py-1.4.32, pluggy-0.4.0
rootdir: D:\work\dev\mypackage, inifile:
collected 0 items / 1 errors
=================================== ERRORS ====================================
__________________ ERROR collecting tests/test_classa.py ___________________
ImportError while importing test module 'D:\work\dev\mypackage\tests\test_classa.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests\test_classa.py:6: in <module>
import dira
E ImportError: No module named dira
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
请注意,我已经使用PyCharm创建了一个测试配置(如魅力)。我错过了什么?
希望你能提供帮助,
Allia
答案 0 :(得分:1)
应为import mypackage.dira
或from ..dira import ???
。