0

Project structure
test/
--test.py
--pkg/
----p1.py
----p2.py

test.py

import pkg.p1  
pkg.p1.print_p1()  

p1.py

import p2  
def print_p1():  
  print('in p1')  
  p2.print_p2() 

p2.py

def print_p2():  
    print('in p2')  

When I run test.py, I gets ModuleNotFoundError: No module named 'p2'.
Tried adding empty __init__.py but still no luck. Any expert can help?
Thanks in advance.

William Fung
  • 1
  • 1
  • 2

2 Answers2

0

Change your p1.py file to this and add a init.py in your pkg directory:

from pkg.p2 import print_p2
def print_p1():
  print('in p1')
  print_p2()
nCoder
  • 88
  • 1
  • 13
0

I found that adding from .p2 import print_p2 into p1.py would make test.py work, though p1.py itself would have error

ImportError: attempted relative import with no known parent package.

Thanks all for your help

buddemat
  • 4,552
  • 14
  • 29
  • 49
William Fung
  • 1
  • 1
  • 2