Packaging your Python Packages into PyPI (pip)

This is a quick walkthrough on submiting your module for a package. Most of it has been distiled from Peter Down’s article so any credits please send it his way. I’ve modified the walkthrough to use setuptools instead of disutils

For a more verbose account, please read his article.

First-time PyPI packagers

  1. Make sure you register an account at both https://pypi.python.org and https://testpypi.python.org
  2. Create a .pypirc file in your ~/.pypirc
[setuptools]
index-servers =
  pypi
  pypitest

[pypi]
repository=https://pypi.python.org/pypi
username=your_username
password=your_password

[pypitest]
repository=https://testpypi.python.org/pypi
username=your_username
password=your_password

PyPI Package directory structure

root-dir/   # arbitrary working directory name
  setup.py
  setup.cfg
  LICENSE.txt
  README.md
  mypackage/
    __init__.py
    foo.py
    bar.py
    baz.py

Note: Your package should belong in a subfolder. In this example, all py files relating to the package is in the mypackage/ folder.

  1. Create a setup.py file. This contains all the important information that you might need
from setuptools import setup
setup(
  name = 'nea_api',
  packages = ['nea_api'],
  version = '0.1.0',
  install_requires=[
        'xmltodict','requests'
  ],
  description = "Python Wrapper for NEA.gov.sg's Data API",
  author = 'David Chua',
  author_email = [email protected]',
  url = 'https://github.com/davidchua/nea_api',
  download_url = 'https://github.com/davidchua/nea_api/tarball/0.1.0',
  keywords = ['data.gov.sg', 'python', 'wrapper', 'psi', 'singapore'],
  classifiers = [],
)

Note: install_requires is where you put your external dependencies. To get your download_url, you need to push a tag. See below.

Tagging
The download_url is a link to a hosted file with your repository’s code. Github will host this for you, but only if you create a git tag. In your repository, type: git tag 0.1 -m “Adds a tag so that we can put this on PyPI.”. Then, type git tag to show a list of tags — you should see 0.1 in the list. Type git push –tags origin master to update your code on Github with the latest tag information. Github creates tarballs for download at https://github.com/{username}/{module_name}/tarball/{tag}.
Taken from Peter Downs’ article

  1. Create a setup.cfg
[metadata]
description-file = README.md

Deploy!

  1. Test it on PyPI Test.
    You want to make sure everything’s working first
$ python setup.py register -r pypitest
$ python setup.py sdist upload -r pypitest

If it all goes well, lets deploy to PyPI Live

  1. Deploy to PyPI Live
$ python setup.py register -r pypi
$ python setup.py sdist upload -r pypi

You’re done!

Reference: