I'm having some trouble with relative imports for the following situation.
I have a package, with two module directories, and I want to import a module from dir_b
to a module from dir_a
.
Here's an example of my package structure:
$ tree
.
├── builder
│ ├── build_moto.py
│ └── __init__.py
├── __init__.py
└── parts
├── car.py
├── __init__.py
├── moto.py
└── truck.py
I'm trying to import moto
inside build_moto
using relative imports, like this:
$ cat builder/build_moto.py
#!/usr/bin/python3
from .parts import moto
...but when I execute build_moto.py
, it generates the following error:
$ python3 builder/build_moto.py
Traceback (most recent call last):
File "builder/build_moto.py", line 3, in <module>
from .parts import moto
SystemError: Parent module '' not loaded, cannot perform relative import
I'd like to understand:
- why this configuration is not working?
- what must be done in order to perform a relative import for this case in specific?