没有名为“__main__.demo"的模块;'__main__' 不是一个包 python3 [英] No module named '__main__.demo'; '__main__' is not a package python3
本文介绍了没有名为“__main__.demo"的模块;'__main__' 不是一个包 python3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我执行 main.py
它工作正常,问题是当我执行 demo2.py
If I execute main.py
it works fine, the problem is when I execute demo2.py
|myPackage
|subPackage
demo.py
demo2.py
main.py
main.py
from ludikDriver.demo2 import demo2_print
demo2_print()
demo2.py
from .demo import demoprint
def demo2_print():
print("demo2")
demoprint()
demo2_print()
演示.py
def demoprint():
print("demo")
错误:from .demo import demoprint
ModuleNotFoundError: No module named '__main__.demo'; '__main__' is not a package
我应该有 __init__.py
吗?
推荐答案
如果您删除 .
,它应该可以工作.demo2.py
变成:
If you drop the .
, it should work. demo2.py
becomes:
from demo import demoprint # instead of `from .demo import demoprint`
def demo2_print():
print("demo2")
demoprint()
demo2_print()
现在你可以在 ipython 中运行 %run ludikDriver/demo2.py
并且你得到:
Now you can run %run ludikDriver/demo2.py
in ipython for instance and you get:
demo2
demo
更多详情请见这篇文章 可能会有所帮助.
For more details, the section "Imports" of this article might help.
这篇关于没有名为“__main__.demo"的模块;'__main__' 不是一个包 python3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文