5

I have Python projects A, B, and C in separate git repos. They're using some similar code in each so I want to refactor the code into a separate, shared repository.The Python code in this repository is really just a few helper classes. I can include the files in this new repo in projects A, B, and C as a git-submodule.

The problem I have now though is that if the code in the git submodule has external pip dependencies, how do the top-level projects resolve those dependencies in addition to their own?

Perhaps git-submodules are not the right approach here, but I really want to avoid setting up a private pypi server for what amounts to 3-4 lightweight modules/classes.

2 Answers 2

3

The problem I have now though is that if the code in the git submodule has external pip dependencies, how do the top-level projects resolve those dependencies in addition to their own?

In your sub-module repository, include your dependencies in requirements.txt as usual.

Then in your documentation, be sure to include instructions on installing the sub-module package before installing A, B, or C.

For an example, lets say that package A is foo, and the sub-module is bar.

tree
.
└── foo
    ├── bar
    │   ├── bar
    │   │   └── __init__.py
    │   ├── requirements.txt # external pip dependencies
    │   └── setup.py
    ├── foo
    │   └── __init__.py
    ├── requirements.txt
    └── setup.py # include 

4 directories, 6 files

Then in your documentation you can include something like this,


Installation for Foo

# Initialize submodule(s)
git submodule update --init --recursive

# First install bar
cd bar

# Resolve any dependencies for bar
pip install -r requirements.txt

# Install bar
python setup.py install

# Now install foo
cd ..

# Resolve any other dependencies for foo
pip install -r requirements.txt

# Install foo
python setup.py install

Note: This should be done for all three repositories, e.g., A, B, and C.


Resources:

Sign up to request clarification or add additional context in comments.

4 Comments

I found out that if I make the submodules python packages themselves with a setup.py, I can install them all in the root ("foo") requirements.txt file as -e bar. Would that be functionally the same as your solution?
Yes, that would be fine.
@swigganicks, could you explain how you managed to do it all at once from the project root, please?
@RémyHosseinkhanBoucher It's been half a decade lol, but I think in your requirements.txt you would put: -e git+ssh://bar -e git+ssh://...
1

Using git submodule foreach makes this very easy:

git submodule --quiet foreach --recursive pip install -r requirements.txt

This also solves any issues you might've with installing local wheels for submodules since this runs from the submodule's directory (and not the main project's directory).

Comments

Your Answer

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.