Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 983
andreas Viena Avatar asked Jul 03 '18 14:07

andreas Viena


People also ask

What does __ main __ mean in Python?

In Python, the special name __main__ is used for two important constructs: the name of the top-level environment of the program, which can be checked using the __name__ == '__main__' expression; and. the __main__.py file in Python packages.


1 Answers

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.

like image 93
Youness Bennani Avatar answered Oct 17 '22 01:10

Youness Bennani