6

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?

2

1 Answer 1

8

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.

1
  • Thats a great article! Thank you. Oct 8, 2019 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.