Making your Python code installable with PIP

I like to install 3rd party Python libraries using pip. Pip and easy_install can automatically download and install Python code from PyPi (also known as The Cheese Shop). This is how to publish your own Python code on PyPi, so people can do this:

pip install yourawesomeproject

The packaging chapter in dive into python 3 is a good starting point, also for Python 2, and this guide is a shorter rehash of that chapter. Just enough to get you started with publishing code on PyPi.

Step 1: Structure your code

Follow the tutorial, and you should have the right structure. Let’s assume that you have the following basic structure for your project “foolib”:

./foolib/
./foolib/foolib/ 
    __init__.py
    foo.py
    bar.py

Add COPYING.txt, README.txt, MANIFEST.in, setup.py:

./foolib/
    COPYING.txt
    README.txt
    MANIFEST.in
    setup.py
./foolib/foolib/
    __init__.py
    foo.py
    bar.py

Example setup.py (choose classifier strings from this list):

from setuptools import setup
 
setup(
    name = 'foolib',
    packages = ['foolib'],
    version = '0.0.1',
    description = 'Does something foo',
    author='Your Name',
    author_email='youremail@example.com',
    url='https://github.com/youraccount/foolib',
    classifiers=[
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'License :: OSI Approved :: GNU General Public License (GPL)',
        'Operating System :: OS Independent',
        'Development Status :: 1 - Planning',
        'Environment :: Console',
        'Intended Audience :: Science/Research',
        'Topic :: Scientific/Engineering :: GIS'
    ]
)

Example MANIFEST.in

include COPYING.txt

Step 2: Build distribution files

This is how to publish a binary distribution of your code on PyPi

# check the configuration
python setup.py check
# build Python eggs and publish in PyPi
python setup.py register bdist_egg upload

This has added some new folders in the root of your library:

./build
./dist
...

And now, anyone can install your Python library using PIP or easy_install

pip install foolib

Leave a Reply