Can you please tell me how to do relative import correctly.
Project Structure:
p1
|- x1
| |- __init__.py
| |- x1_module1.py
|- x2
|- __init__.py
|- x2_module1.py
In x2_modules.py
try:
from p1.x1.x1_module import temp_func
except Exception as e:
print('Failed "from p1.x1.x1_module import temp_func"')
print(e)
try:
from .x1.x1_module import temp_func
except Exception as e:
print('Failed "from .x1.x1_module import temp_func"')
print(e)
try:
from ..x1.x1_module import temp_func
except Exception as e:
print('Failed "from ..x1.x1_module import temp_func"')
print(e)
Output:
Failed "from p1.x1.x1_module import temp_func"
No module named 'p1'
Failed "from .x1.x1_module import temp_func"
attempted relative import with no known parent package
Failed "from ..x1.x1_module import temp_func"
attempted relative import with no known parent package
[Finished in 0.2s]
p1
was in the path, this would all work becausep1
would be recognized as a package. – tdelaney Oct 24 '20 at 5:01