I have a complex repository structure with multiple sub-packages:
/foo/file1.py
/foo/file2.py
/foo/bar/file3.py
/common/shared.py
(very simplified version)
All package references are either explicitly relative or absolute from the root.
Everything works fine, but I need to run all scripts as modules using their full name from the root, e.g.: python -m foo.file1
. The coding guideline for the repository is and will remain to use this pattern to run scripts.
Yet, is there a way to make a few files runnable as a script in this setup?
For example, I wish I could do:
if not __package__:
__package__ = "foo.bar"
# use relative or absolute imports as if the file was run with python -m
I already tried importlib
or runpy
without success. Using sys.path.append
makes absolute imports valid but relative imports are still an issue.