Python Packaging

From Littledamien Wiki
Revision as of 16:58, 30 March 2018 by Video8 (talk | contribs)
Jump to navigation Jump to search

Overview

Create python packages that can be distributed with pip from GitHub (and not PyPi).

Configuration

A setup config file, setup.py, in the repo root directory should contain a single call to setuptools.setup(), like so: [1]

from setuptools import setup

setup(name='namespace-my-package',
      version='0.1',
      description='My package description',
      url='http://github.com/dbarchowsky/namespace-my-package',
      author='Damien Barchowsky',
      author_email='dbarchowsky@gmail.com',
      license='MIT',
      packages=['namespace.my_package'],
      install_requires=['list', 
            'of', 
            'dependencies',
            ],    
      zip_safe=False)

After which the package can be installed locally with

$ pip install .

And once installed the package can be used like this:

>>> from namespace.my_package import my_module
>>> my_module.some_routine()

Namespaces

It's recommended to use namespaces when creating packages. [2] Even if the package is not being publicly distributed, the names of dependencies from other packages can clash.

Installation from GitHub

Prerequisites

Create and store a local SSH key. (One each for all computers where development is happening.) See Generating a new SSH key and adding it to the ssh-agent (GitHub Help)

Register the key on the GitHub account. See Adding a new SSH key to your GitHub account (GitHub Help)

One key pair will suffice for all ssh requests, i.e. for a computer with the key installed, the key can be used for all repositories in the GitHub account.

Installing a package

To install a package from a private GitHub repository: [3]

$ pip install git+ssh://git@github.com/username/my-repo.git

The packages dependencies are listed under install_requires=[] in the package's setup.py.

Updating a package

After changes have been committed to a repository, they can be retrieved with:

$ pip install git+ssh://git@github.com/username/my-repo.git --upgrade

(or) 

$ pip install git+ssh://git@github.com/username/my-repo.git -U

See also