Installing a Python package from Github

While the standard route for installing a Python package would be to upload the package to PyPI and then use pip, clearly some packages are too narrow in scope for this. For example, why should the rest of the world care about Python code for generating efficiency plots for SALT?

But still… using pip would be useful even for such packages, as you might need them in different locations. And you can.

To illustrate the process, we’ll create and “distribute” a very simple package, which is offering a function for displaying a Numpy vector of integers. Start by creating a parent folder, say MyApp/, and in there create a Python package saao_vector including a file vector.py with the following content.

import numpy as np

def display_vector():
    print(str(np.arange(10)))

The details required for installing this package must be included in a file setup.py, which should look like the following.

from setuptools import setup

readme = file('README.txt')
readme_text = ''
for line in readme:
    readme_text += line

setup(name='saao_vectors',
      version='0.1',
      url='http://www.saao.ac.za',
      description='nonsensical vector package for testing',
      long_description=readme_text,
      packages=['saao_vectors'],
      install_requires=['numpy'],
      author='Christian Hettlage',
      author_email='hettlage@saao.ac.za')

If we had a package a.b, you might have to include both 'a' and 'a.b' in the packages array. Note that we have defined NumPy as an installation requirement, which will make pip install it automatically.

The next step is to create a repository on Github and put the content of MyApp folder (but not the folder itself) there. setup.py should be at the top level of the repository.

With all this in place you can include the package in your project by adding its Github address to your projects requirements.txt file:

some_package
some_other_package
git+https://github.com/hettlage/SaaoVectors.git#egg=saao_vectors

Note the git+https in the Github address line.

If you want to install from a local repository, you would use

git+file:///path/to/your/repo

When running pip with the requirements file

pip install -r requirements.txt

the package is installed along with NumPy.