I have a python program that relies on modules. The structure is the following.
program.py
modules/__init__.py
modules/mod1.py
modules/mod2.py
modules/utils.py
program.py
includes all modules in the modules/
directory and the modules in modules/
include modules/utils.py
via from .utils import *
The above works fine when running program.py
. However, mod1.py
and mod2.py
also work as their own scripts, but running python mod1.py
gives the error
from .utils import *
ModuleNotFoundError: No module named '__main__.utils'; '__main__' is not a package
Removing the .
from the import makes the individual scripts work fine, but then the main program.py
script can not find utils
.
How can I make the import work for both the main program and the individual modules?