How to Effectively Manage “git submodule” Dependencies in Your Project
Manage “git submodule” dependencies in your project using Pipenv.
Managing external dependencies, with strict control over versions becomes simpler when using git submodules. In a recent project, I needed to do precisely this. I used Pipenv as my package manager and the submodule entry in my project’s Pipfile was as follows:
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"[packages]
loguru = "*"
requests = "*"
tool_kit = {editable = true, path = "./submodules/tool_kit"}[dev-packages]
pytest = "*"
requests-mock = "*"[requires]
python_version = "3.9"tool_kit is an example of an external dependency.
If you are looking to add one or more git submodules to your project, this article will serve as a good reference.
Prerequisites
Python 3.9, pip==21.1.3 and Pipenv==2021.5.29 or later
Add The Required Submodule to Your Pipfile
My primary project had a subdirectory named submodules that contained all external dependencies and libraries needed by my application.
I added the git submodule, tool_kit, to my project as follows:
git submodule add /path/to/tool_kit.git submodules/tool_kitExecuting this command created a .gitmodules file as follows:
[submodule "submodules/tool_kit"]
path = submodules/tool_kit
url = /path/to/tool_kit.gitRunning the following command updated my project’s Pipfile.lock with the transitive dependencies for the submodule:
pipenv install --keep-outdated -e ./submodules/tool_kitAdding the Pipfile, Pipfile.lock and the .gitmodules files to your project will make it simple for other users to clone and use your application.
Looking To Creating Your Own Submodule?
Be sure to correctly set up your project to avoid painful dependency issues.
Here is a sample setup.py file for our example project tool_kit:
import setuptoolssetuptools.setup(
name="tool_kit",
version="0.1",
packages=["tool_kit"],
install_requires=[
"requests",
"pandas",
"loguru",
"pyarrow",
"croniter",
],
python_requires=">=3.9",
)Note: Running pipenv install — keep-outdated -e ./submodules/tool_kit will generate a generic pyproject.toml file in your submodule’s directory with a =40.8.0", "wheel"] build-backend = "setuptools.build_meta:__legacy__"">setup.py.
Thanks for reading. If you liked this article, please leave (up to 50) claps and consider subscribing to get the best of my articles delivered straight to you.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Check out our Community Discord and join our Talent Collective.






