In my folder named project
, this is my directory structure.
.
├── folder_1
│ ├── folder_3
│ │ ├── module_3.py
│ │ └── module_4.py
│ └── module_1.py
├── folder_2
│ └── module_2.py
└── script.py
I am trying to do relative imports inside module_3.py
# module_3.py
print("Code is currently in module_3.py file")
print("n File name of module_3.py :",__name__)
from . import module_4
from .. import module_1
from ...folder_2 import module_2
-
When I do
python3 -m project.folder_1.folder_3.module_3
, it runs successfully. -
When I run
python3 -m folder_1.folder_3.module_3
, I am getting below error:
Traceback (most recent call last):
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "/Users/admin/Desktop/remote/project/folder_1/folder_3/module_3.py", line 13, in <module>
from ...folder_2 import module_2
ImportError: attempted relative import beyond top-level package
What I know is that relative imports works inside packages only.
QUESTION – Is my understanding correct that giving project
in command helps python understand that project
is also a package, otherwise it considers only folder_1
a package and cannot find folder_2
even if it is at the same level as folder_1
because it does not recognize its parent folder project
directory as a package since I am running the command excluding project
word from it.
Does this mean it is always better to run your program with the topmost parent folder name in your command hence telling python the top most package.
If not so, what is the reason it cannot traverse folder_2
which is a sibling directory at the same level as folder_1