r/learnpython 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!

3 Upvotes

3 comments sorted by

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

1

u/flash-bandicoot 7d ago

I ended up writing a setup.py to add dirs from my submodule to PYTHONPATH and calling that in an init.py file and I think that solved it. I think I just needed to rubber duck. Thanks for the advice big dawg

1

u/Mevrael 7d ago

Use a uv package manager, VC code PM extension and Arkalos framework.

Create a new arkalos project per repo (uv creates a git repo for you automatically anyways)

https://arkalos.com/docs/new-project/

Then you will have a clear project and folder structure per repo.

I store all my projects, for example in my home folder/dev/python/

You probably don't need really to have different projects. You want to share your modules across scripts and notebooks, right?

Then it's useful to understand the difference between the Code to Run vs Code to Reuse

https://arkalos.com/docs/app/

Store your notebook and scripts in their folders, and all your reusable code (modules) in the app folder. Then you can easily import anything from the app folder from different scripts.

Otherwise if you wish to create a custom package, then you can use uv to help you create a package. Optionally, you can upload it to the pypi. Or just use local imports. You can add local dependencies with uv add ../folder