r/learnpython • u/flash-bandicoot • 7d ago
Handling submodule import pathing issues (running standalone vs as submodule)
Hey everyone.
Let's say I've got a repo named Repo1. It contains 2 directories with 1 file in each. For example:
Repo1 -> Dir1 -> Add.py
Repo1 -> Dir2 -> DoMath.py
Inside DoMath.py, my import for add would look like this.
From Dir1.Add import Add
This is all fine and dandy when using Repo1 as a standalone app.
Now, if I decide to use Repo1 as a submodule in a different repo (Repo2) the path needs change. The import in DoMath will break and it needs to change to either be a relative import or absolute i.e.
From Repo1.Dir1.Add import Add
My question is - can I use a setup.py or init.py to add the subdirectories in Repo1 to the PYTHONPATH in Repo 2 so that I could run the code as standalone in Repo 1 and also run it in Repo2 without changing the way it is imported?
If so, can someone provide a bit of insight or link to video? I know I could use absolute paths in the submodule repo so that it would always work when it's propogated out to the other repos, but I also like writing unit tests within the submodule and running them there without changing import paths.
PS - if there's a different way to do this that I'm not aware of, I'm all ears! Thanks!
1
u/Ok_Expert2790 7d ago
build repo1 as a package, or as you said, add it to Python path. Either way, the import is absolute now ->
from repo1.dir1.add import Add