How to pip install from a Git repository subdirectory
Codemia Knowledge Hub ·
Point pip at the Git repository and revision, then add subdirectory= to the URL fragment so it knows which folder holds the package metadata. This is common in monorepos with several packages or non-Python code: as long as the subdirectory has a valid pyproject.toml or other supported build metadata, pip installs it like any other package.
The classic #egg= VCS URL
The pieces are:
- the Git URL
- the branch, tag, or commit after
@ - the package name after
#egg= - the nested package path after
subdirectory=
If the subdirectory is wrong, pip may clone the repository successfully and then fail because it cannot find package metadata. That failure tells you Git access worked and the remaining bug is the package path.
The direct-reference form
Modern pip also supports a more readable direct-reference form:
It reads well in requirements.txt because the dependency name comes first, and it is not tied to the older #egg= convention:
Pin a tag or commit for reproducible installs
Installing from @main is convenient during development but makes builds non-deterministic. Use a tag or commit hash so the same command always resolves to the same code:
This matters most in CI and deployment pipelines, where a moving branch can make "the same build" produce different environments on different days.
Point at the actual package root
The target folder must contain a valid build configuration, such as:
pyproject.tomlsetup.pysetup.cfgwith the appropriate build metadata
For example:
Here the correct value is subdirectory=python/sdk, not subdirectory=python. Only the selected package is installed, not everything in the repository.
Private repositories
Use an authentication method that fits the environment. SSH is a common choice when deploy keys or user keys already exist:
Do not hard-code secrets in requirements files or URLs. In CI, prefer short-lived tokens or managed SSH keys supplied by the platform. If installation fails early, test access with a plain git clone first to separate Git access problems from pip packaging problems.
Related reading
- How can I install packages using pip according to the requirements.txt file from a local directory?
- How can I Install pip for python 3.7 on Ubuntu 18?
- How can I iterate over overlapping current, next pairs of values from a list?
- How can I iterate over rows in a Pandas DataFrame?
- How can I keep my fork in sync without adding a separate remote?
- How can I know if a branch has been already merged into master?
- How can I list or discover queues on a RabbitMQ exchange using python?
- How can I list the contents of a directory in Python?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.