How to write setup.py to include a Git repository as a dependency How to write setup.py to include a Git repository as a dependency python python

How to write setup.py to include a Git repository as a dependency


After digging through the pip issue 3939 linked by @muon in the comments above and then the PEP-508 specification, I found success getting my private repo dependency to install via setup.py using this specification pattern in install_requires (no more dependency_links):

install_requires = [  'some-pkg @ git+ssh://git@github.com/someorgname/pkg-repo-name@v1.1#egg=some-pkg',]

The @v1.1 indicates the release tag created on github and could be replaced with a branch, commit, or different type of tag.


Note: this answer is now outdated. Have a look at this answer below from @Dick Fox for up-to-date instructions: https://stackoverflow.com/a/54794506/2272172


You can find the right way to do it here.

dependency_links=['http://github.com/user/repo/tarball/master#egg=package-1.0']

The key is not to give a link to a Git repository, but a link to a tarball. GitHub creates a tarball of the master branch for you if you append /tarball/master as shown above.


This answer has been updated regularly as Python has evolved over the years. Scroll to the bottom for the most current answer, or read through to see how this has evolved.

Unfortunately the other answer does not work with private repositories, which is one of the most common use cases for this. I eventually did get it working with a setup.py file that looks like this (now deprecated) method:

from setuptools import setup, find_packagessetup(    name = 'MyProject',    version = '0.1.0',    url = '',    description = '',    packages = find_packages(),    install_requires = [        # Github Private Repository - needs entry in `dependency_links`        'ExampleRepo'    ],    dependency_links=[        # Make sure to include the `#egg` portion so the `install_requires` recognizes the package        'git+ssh://git@github.com/example_org/ExampleRepo.git#egg=ExampleRepo-0.1'    ])

Newer versions of pip make this even easier by removing the need to use "dependency_links"-

from setuptools import setup, find_packagessetup(    name = 'MyProject',    version = '0.1.0',    url = '',    description = '',    packages = find_packages(),    install_requires = [        # Github Private Repository        'ExampleRepo @ git+ssh://git@github.com/example_org/ExampleRepo.git#egg=ExampleRepo-0.1'    ])

However, with the very latest pip you'll run into issues with the EGG format handler. This is because while the egg is ignored pip is now doing direct URL matching and will consider two URLs, one with the egg fragment and the other without, to be completely different versions even if they point to the same package. As such it's best to leave any egg fragments off.

So, the best way (current to June 2021) to add a dependency to Github that will work with public and private repositories:

from setuptools import setup, find_packagessetup(    name = 'MyProject',    version = '0.1.0',    url = '',    description = '',    packages = find_packages(),    install_requires = [        # Github Private Repository        'ExampleRepo @ git+ssh://git@github.com/example_org/ExampleRepo.git'    ])