my_module.py
is a file in a package I'm developing. Sometimes I want to run it on its own (python -i my_module.py
) for debugging purposes.
from .other_module import x, y
-syntax relative imports fail in the case of running it directly.
I've worked around the problem by filling the top of every file in my package with these kinds of statements instead of normal imports:
try:
from .other_module import x, y
except ImportError:
from other_module import x, y
but now I have the ugliest preamble I've ever seen.
How is this kind of problem meant to be resolved? I just want to import relative modules no matter whether I am currently being run as a module in a package, or being run for debugging purposes on my own.