Home > Enterprise >  Nested python import
Nested python import

Time:01-05

How does python handle nested modules? I have a main.py file that's trying to call a function from a submodule that imports it's function from a sister module. I'm getting a "ModuleNotFoundError: No module named 'c' error. I've tried adding an empty __ init __.py to each directory.

main.py    
|_sub/
  |_b.py
  |_c.py

# main.py
from sub.b import do_b
  do_b()

# b.py
from c import do_c
def do_b():
  do_c()

# c.py
def do_c():
  print("hello from c")

CodePudding user response:

Add . before module c to indicate it is in current directory

# b.py
from .c import do_c
def do_b():
  do_c()
  • Related