I am working on a project which has the following structure:
project
├── config.py
└── modules
└── a.py
According to PEP 328 relative imports are possible.
However when I start Python (in shell) in the same directory as a.py
and execute the following commands:
from ..config import *
from ...project import *
I receive the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: Parent module '' not loaded, cannot perform relative import
I understand that import *
is not recommended, it is only being used for testing (trying to import config).
Relative imports can only work where the module was itself imported. Running that command in an interactive session has no parent module. Also, the current directory is implicitly search (thus treated like a package) and so import a
from an interactive interpreter in that directory will have no parent module.
So, to test, set PYTHONPATH to the root of your project (as a temporary measure), Then do from modules import a
from another directory, such as your home directory. The a
module may then do a from ..config import *
, and only then.