I have a file tree that looks like this:
my_repo/
|-- main/
| |-- my_script.py
| |-- __init__.py
|-- common/
| |-- utils.py
| |-- __init__.py
|-- config/
| |-- config.py
| |-- __init__.py
and I have installed an external package with a file tree:
some_package
|-- setup.py
|-- config.py
...
After installing the package, by default running import config
will import the module from some_package
.
my_script.py
needs to import common/utils.py
which runs the following:
from config.config import SOME_VALUE
No matter how I try to rearange the imports path, I still get the error:
ModuleNotFoundError: No module named 'config.config'; 'config' is not a package
The above error clearly means that we were able to find the common
package in the path but config
is still imported from some_package
Here are my attempts to import common/utils.py
in my_script.py
, all yielding the same result:
sys.path.append('..'); from common import utils
sys.path.append('..'); from common import utils
sys.path.insert(0, '/home/myuser/my_repo'); from common import utils
sys.path.insert(0, '..'); from common import utils
sys.__plen = len(sys.path); sys.path.append('..'); new=sys.path[sys.__plen:]; del sys.path[sys.__plen:]; p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new); from common import utils
The last idea is take from here.
Anyone got any fresh ideas on how to give priority to my config package instead of the one from the external package?
My python version is 3.9.7. Thanks