How to submit a package to PyPI

By | October 29, 2015

How to submit a package to PyPI

PyPI (Python Package Index) is a repository of software for the Python programming language. This article will tell you how to submit your package to PyPI, so others are able to install it with easy_install or pip. The offical document is at http://wiki.python.org/moin/CheeseShopTutorial#Submitting_Packages_to_the_Package_Index

Create your accounts

You must create accounts on PyPI Live and PyPI Test before upload your code.

After registration, create a pypirc configuration file at ~/. This file holds your information for authenticating with PyPI, both the live and the test versions.

[distutils]
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}}

Prepare your package

Every package on PyPI needs to have a file called setup.py at the root of the directory. If you are using a markdown-formatted README file you’ll also need a setup.cfg file. Also, you’ll want a LICENSE.txt file describing what can be done with your code. So the directory structure would look like this:

root-dir/
  setup.py
  setup.cfg
  LICENSE.txt
  README.md
  mypackage/
    __init__.py
    file1.py
    file2.py
    file3.py

setup.py

This is metadata about your library.

from distutils.core import setup
setup(
  name = 'mypackage',
  packages = ['mypackage'],
  version = '0.1',
  description = 'A random test lib',
  author = 'Peter Downs',
  author_email = 'your email address',
  url = 'https://github.com/yfpeng/mypackage',
  keywords = ['testing', 'logging', 'example'],
  classifiers = [],
)

setup.cfg

This tells PyPI where your README file is.

[metadata]
description-file = README.md

Upload your package to PyPI

Run the following code to
1. Register your package against PyPI (and test) server, to make sure you’ve set up everything correctly.
2. Upload the package to PyPI (and test) server

python setup.py register -r pypitest
python setup.py sdist upload -r pypitest
python setup.py register -r pypi
python setup.py sdist upload -r pypi

Leave a Reply

Your email address will not be published. Required fields are marked *