[英]"no module named..." when running this python code
I have no idea what causes this error, this is as simplest as I could get, yet SO gives me hint that my question does not meet their quality standards so I must post this longer intro.我不知道是什么导致了这个错误,这是我能得到的最简单的方法,但 SO 提示我我的问题不符合他们的质量标准,所以我必须发布更长的介绍。
#===
# my_script.py
import myModule
#===
# myModule\__init__.py
import sys
from my_def import *
class MyModule:
def __init__(self):
pass
#===
# myModule\my_def.py
def my_def():
pass
Traceback (most recent call last):
File "my_script.py", line 1, in <module>
import myModule
File "D:\test\myModule\__init__.py", line 2, in <module>
from my_def import *
ImportError: No module named 'my_def'
你可以试试这个:
sys.path.append("path_of_script")
In __init__.py
, add a .
在
__init__.py
,添加一个.
before my_def
.在
my_def
之前。 The .
.
means that it should be looking for the file in the same folder as the script itself, rather than relative to my_script.py
.意味着它应该在与脚本本身相同的文件夹中查找文件,而不是相对于
my_script.py
。
from .my_def import *
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.