[英]ImportError: No module named test when ___init__.py is included
I created a python project in this format: 我以这种格式创建了一个python项目:
I tried to run my test_jabba.py
by cd
ing into the tests
directory and running the program and I received this error: 我试图运行我的
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
I read around and I realized I needed __init__.py
to tell python where other packages are located. 我四处阅读,意识到我需要
__init__.py
来告诉python其他包在哪里。
Top portion of test_jabba.py
test_jabba.py
from tests import testbench
from utils import gopher, jsonstream
I did not add __init__.py
into my logs
and resources
directories as they do no contain any python. 我没有将
__init__.py
添加到我的logs
和resources
目录中,因为它们不包含任何python。
My best guess would be that poc
is not in your PYTHONPATH
. 我最好的猜测是
poc
不在您的PYTHONPATH
。 You can either set/extend the environment variable to contain poc
, or you can manipulate the path in your script using os.path
. 您可以将环境变量设置/扩展为包含
poc
, 也可以使用os.path
在脚本中操作路径 。 Your imports, in this case, will have to change accordingly: 在这种情况下,您的进口商品将必须进行相应的更改:
from poc.tests import testbench
from poc.utils import gopher, jsonstream
Alternatively, you can use a relative import, to import tests
and utils
: 另外,您也可以使用相对导入,导入
tests
和utils
:
from ..tests import testbench
from ..utils import gopher, jsonstream
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.