I have code structured like this, where the some_lib is supposed to be installed with pip/setuptools
mainApp.py
submodule
|some_lib
|__init__.py
|some_lib.py
|helpers
|helpers.py
and I want to import the some_lib.py from mainApp.py.
I can do easily if I have structure like this:
mainApp.py
some_lib
|__init__.py
|some_lib.py
|helpers
|helpers.py
however my goal is to have this library to be added as a submodule, and be able override the one installed in the system.
The issue is that the some_lib wants to import things like this: from some_lib.helper import SomeStuff, which leads to the library installed in the system with pip, since my package is submodule.some_lib.helper
I can workaround it with adding the library location to the search path with
import sys
sys.path.insert(0, my_lib_location)
but this doesn't seem correct to me, and I think will lead to issues later. I'd like to know if there's other way to do it, without touching the search paths. Also, I want to keep this compatible with other versions, and be installed, so I want to avoid modyfing the the imports in the library.
1 Answer 1
You need to add an __init__.py in the same level of the submodule and import the lib in that. You can also leave the __init__.py inside the some_lib empty,
mainApp.py
|__init__.py
|submodule
|some_lib
|__init__.py
|some_lib.py
|helpers
|helpers.py
This is the repository link for template multi level python package.
5 Comments
ModuleNotFoundError: No module named 'some_lib' when the lib tries to import with some_lib