1

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.

asked Jun 14, 2021 at 13:15

1 Answer 1

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.

answered Jun 14, 2021 at 13:30
Sign up to request clarification or add additional context in comments.

5 Comments

I've realized I messed the stucture, I've edited to the correct one. I did try that, and no success, fails with ModuleNotFoundError: No module named 'some_lib' when the lib tries to import with some_lib
@Rychu I dont know how do you import the some_lib in the outer init. You should import it like that: from .some_lib import [a function in the some_lib]. Do you do the same thing?
Hi, sorry for late response. The issue was that the lib itself assumed it's on the top level search path, and has non-relative imports inside. I did want to avoid both meddling with the search paths and modyfying the lib, but I guess the answer is 'no, there's no way to do, just that pick either one of the mentioned solutions'.
and just to be clear - I did try that import, but it did not solve my problem, but it's quite likely that I don't understand something about the python imports. For now I have used a workaround, but I'll try to check in isolation if I can get this to work with your suggestions, I'll mark the answer as a solution.
@Rychu Sorry for late answer. I have created a template multi level python package, because I think it is tricky and without template it is difficult to start with. I have update the answer with the repo link. Feel free to make changes in the repo, add files or function. We can fix this together.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.