ImportError:包含___init__.py时没有名为test的模块

时间:2015-04-02 23:44:38

标签: python

我用这种格式创建了一个python项目:

enter image description here

我尝试将test_jabba.py cd运行到tests目录并运行程序,我收到了此错误:

Traceback (most recent call last):
  File "./test_jabba.py", line 12, in <module>
    from tests import testbench
ImportError: No module named tests

我读了一遍,我意识到我需要__init__.py来告诉python其他软件包的位置。

test_jabba.py的顶部

from tests import testbench
from utils import gopher, jsonstream

我没有将__init__.py添加到我的logsresources目录中,因为它们不包含任何python。

1 个答案:

答案 0 :(得分:1)

我最好的猜测是poc不在PYTHONPATH。您可以设置/扩展环境变量以包含poc,或 you can manipulate the path in your script using os.path。在这种情况下,您的导入必须相应更改:

from poc.tests import testbench
from poc.utils import gopher, jsonstream

或者,您可以使用相对导入来导入testsutils

from ..tests import testbench
from ..utils import gopher, jsonstream