I've looked at so many stackoverflow posts but I don't understand why I'm getting this problem
My file system
parent_folder
|folder1
|module1.py
|module2.py
|__init__.py
|folder2
|main.py
I want to import module1.py and module2.py into main.py.
My __init__.py file has the following
__all__=['module1','module2']
Here is what I'm trying in main.py
from ..folder1 import *
But this give me this error:
Traceback (most recent call last):
File "main.py", line 2, in <module>
from ..folder1 import *
SystemError: Parent module '' not loaded, cannot perform relative import
I've been trying to solve this for an hour and nothing is working. Please help.
edit: I solved this by using absolute path (nothing else was working)
Relative imports works with respect to your package.
When you run python main.py
simply it cant find what is current package
considering your path to parent folder is /path/up/to/parent_folder
execute
python -m parent_folder.folder2.main from /path/up/to
OR
set PYTHONPATH
or sys.path
to /path/up/to/parent_folder
, then change import to following
from folder2 import *
and execute python main.py
UPDATE
One of relative imports use case is to consume your package
from script outside the package. This script usually acts as entry point or executable.
following is an example directory structure
myapp
│ entry.py
│
└───package
│ __init__.py
│ __init__.pyc
│
├───folder1
│ module1.py
│ __init__.py
│
└───folder2
start.py
__init__.py
myapp/entry.py:
import sys
from package.folder2 import start
if __name__ == '__main__':
sys.exit(start.main())
myapp/folder2/start.py:
import sys
from ..folder1.module1 import *
# this is relative import to "package".
# Use?: It will avoid confusion and conflicts with other packages in your syspath. Improve readability at some extent,
def main():
print "do something with folder1.module1 imported objects"
and $ python entry.py