pip
git
subdirectory
install
Python

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

bash
pip install "git+https://github.com/example/monorepo.git@main#egg=my_package&subdirectory=python/sdk"

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:

bash
pip install "my_package @ git+https://github.com/example/[email protected]#subdirectory=python/sdk"

It reads well in requirements.txt because the dependency name comes first, and it is not tied to the older #egg= convention:

text
my_package @ git+https://github.com/example/[email protected]#subdirectory=python/sdk

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:

bash
pip install "my_package @ git+https://github.com/example/monorepo.git@3f6b2f8#subdirectory=python/sdk"

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.toml
  • setup.py
  • setup.cfg with the appropriate build metadata

For example:

text
1repo/
2  python/
3    sdk/
4      pyproject.toml
5      src/
6        my_package/

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:

bash
pip install "my_package @ git+ssh://[email protected]/example/private-repo.git@main#subdirectory=python/sdk"

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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms