Include GitHub repository in requirements.txt

Overview

English How to include a public library in a dependency on GitHub, although it is not registered in PyPI. In addition, we will summarize as a memorandum how to deal with the case where requirements.txt is referenced from setup.py.

requirements.txt If the URL of the GitHub repository is https://github.com/rgmining/common requirements.txt contains

requirements.txt


-e git+https://github.com/rgmining/common.git#egg=rgmining_common-0.9.0

Write like this. After # egg =, it seems to be in the format -. Also, if you are using pip-tools to generate requirements.txt Enter the same character string as above in requirements.in.

setup.py Until now, setup.py passed the contents of requirements.txt to ʻinstall_requires` as shown below.

setup.py


from setuptools import setup, find_packages

def load_requires_from_file(filepath):
    with open(filepath) as fp:
        return [pkg_name.strip() for pkg_name in fp.readlines()]

setup(
    #Other items omitted
    install_requires=load_requires_from_file("requirements.txt")
)

If the URL is included in requirements.txt, improve it a little and list only the package name.

setup.py


def take_package_name(name):
    if name.startswith("-e"):
        return name[name.find("=")+1:name.rfind("-")]
    else:
        return name.strip()

def load_requires_from_file(filepath):
    with open(filepath) as fp:
        return [take_package_name(pkg_name) for pkg_name in fp.readlines()]

Also, pass the URL part to the keyword argument dependency_link of the setup function.

setup.py


def load_links_from_file(filepath):
    res = []
    with open(filepath) as fp:
        for pkg_name in fp.readlines():
            if pkg_name.startswith("-e"):
                res.append(pkg_name.split(" ")[1])
    return res

setup(
    #Other items omitted
    install_requires=load_requires_from_file("requirements.txt"),
    dependency_links=load_links_from_file("requirements.txt"),
)

With the above, if you do python setup.py test etc., you can prepare a package from GitHub and execute the test.

reference

-Install github repository with pip

Recommended Posts

Include GitHub repository in requirements.txt
Extract each Location from Stargazers in the Github repository
Install github repository with pip
Pip install the GitHub repository
Github Interesting Repository ⓪ Table of Contents
Include "%" in argparse help to die