Pip Installs From Github

Pip installs From github

Installing a package from github is fairly simple.  The following are examples of installing packages from github.

# Install repo package from the url, master branch
pip3 install git+https://github.com/username/repo.git
@ Install repo from the specified branch
pip3 install git+https://github.com/username/repo.git@branch
# Install repo from the specified commit.
pip3 install git+https://github.com/username/repo.git@commit

requirements.txt

At some point, you will find yourself wanting to list a dependency in the requirements.txt file that resides on github.  This is fairly straight forward If you only plan to use it in requirements.txt (not processed for usage in setup.py).  Note that I’ve specified the master branch of repo and given it a version id.

https://github.com/username/repo/tarball/master#egg=repo-0.1.1

setup.py and requirements.txt

Some people will process their requirements.txt files to generate the install_requires parameter for the setup function called in setup.py.  This works fine until you have a repository on github.  Setup will fail to find your dependencies if your requirements.txt has a line like the one above.  To remedy this we must do two things.

  1. Parse the line to create a named python dependency for install requires. Given the file above, install_requires would equal [“repo==0.0.1”] .
  2. Specify the dependency_links argument to setup.  [“https://github.com/chaddotson/repo/tarball/master#egg=repo-0.1.1”]  for this example.

An Example

This is an example of a setup.py that properly processes requirements.txt dependencies that are located on github.  It’s probably not complete, but it works for what I need.  Feel free to take and adapt.  See the repo here (python3).

from setuptools import setup
version = '0.1.0'
temp_install_reqs = []
install_reqs = []
dependency_links = []
with open("requirements.txt", "r") as f:
    temp_install_reqs = list(map(str.strip, f.readlines()))
for req in temp_install_reqs:
    # This if should be expanded with all the other possibilities that can exist.  However, this
    # simple version works for this program.
    if req.startswith("https://"):
        dependency_links.append(req)
        install_reqs.append(req[req.find("egg=") + 4:].replace("-", "==", 1))
    else:
        install_reqs.append(req)
setup(
    name='tweeter-repeater',
    version=version,
    packages=['bin'],
    url='',
    license='',
    author='Chad Dotson',
    author_email='chad@cdotson.com',
    description='',
    install_requires=install_reqs,
    dependency_links=dependency_links,
    entry_points={
        'console_scripts': [
            'tweeter-repeater = bin.main:main',
        ]
    },
)

 
 
 
 

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply