No module named '__main__.demo'; '__main__' is not a package python3

浪子不回头ぞ 提交于 2019-12-06 02:44:29

问题


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()

demo.py

def demoprint():
    print("demo")

Error: from .demo import demoprint

ModuleNotFoundError: No module named '__main__.demo'; '__main__' is not a package

Should I have __init__.py?


回答1:


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()

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.



来源:https://stackoverflow.com/questions/51157314/no-module-named-main-demo-main-is-not-a-package-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!