I have the following directory structure:
some-tools-dir/
base_utils.py
other_utils.py
some-tool.py
some-other-tool.py
some-other-tools-dir/
basetools -> symlink to ../some-tools-dir
yet-another-tool.py
In other_utils.py
, I have:
import base_utils
Now, in yet-another-tool.py
, I want to do:
import basetools.other_utils
That doesn't work, because Python does not recognize basetools
as a Python package.
So I add an empty basetools/__init__.py
.
Now, in other_utils
, I get the exception:
import base_utils
ImportError: No module named base_utils
So I change that line to:
from . import base_utils
And yet-another-tool.py
works now.
However, some-tool.py
does not work anymore. It imports other_utils
, and there I get the exception:
from . import base_utils
ValueError: Attempted relative import in non-package
Now, I can add this hack/workaround to some-tools-dir/*-tool.py
:
import os, sys
__package__ = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
sys.path += [os.path.dirname(os.path.dirname(os.path.abspath(__file__)))]
__import__(__package__)
And in addition, make all local imports relative in those files.
That solves the problem, I guess. However, it looks somewhat very ugly and I have to modify sys.path
. I tried several variations of this hack, however, I want to support multiple Python versions if possible, so using the module importlib
becomes complicated, esp. because I have Python 3.2, and I don't like using module imp
because it's deprecated. Also, it only seems to become more complicated.
Is there something I'm missing? This all looks ugly and too complicated for a use-case which doesn't seem to be too uncommon (for me). Is there a cleaner/simpler version of my hack?
A restriction I'm willing to make is to only support Python >=3.2, if that simplifies anything.