0

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

BelgishChoko
  • 393
  • 2
  • 12
  • 1
    is there a way you can provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example)? – Almog-at-Nailo Sep 26 '21 at 09:18
  • 2
    And please include how you've installed `some_package`, as `import config` should not import a module from inside a package. – Jasmijn Sep 26 '21 at 09:21
  • "sys.modules" contains all already imported modules. On import Python looks it up first. It can help to manipulate it. – Michael Butscher Sep 26 '21 at 09:25
  • reproduce using: https://github.com/guyazran/mystery_issue/tree/master/my_repo. installed "some_package" by cd some_package; python setup.py build develop – BelgishChoko Sep 26 '21 at 19:11

1 Answers1

0

Add path-to-project-root/my_repo to your PYTHONPATH before to run your program. From there, when you ask Python to import something, it will find it in the my_repo directory (also because they each contain an __init__.py, see this post).
Having a correct PYTHONPATH is a simple solution which works for nearly every project.

You can also edit the sys.path list when the program starts, but take care that when you append('..') it is relative to your current directory os.curdir, which may or may not be what you want. It changes depending on how you execute your program.

You can check what you are actually doing by printing both sys.path and os.path.abspath(os.curdir) at program startup.

Lenormju
  • 4,078
  • 2
  • 8
  • 22