Codementor Events

How to Host your Python Package on PyPI with GitHub

Published Jun 01, 2016Last updated Jan 18, 2017
How to Host your Python Package on PyPI with GitHub

PyPI is the official Python Packages Index. Once you publish it on PyPI, then it can be downloaded via a simple command pip install <package>. Life becomes simpler with this one line.

1: Create accounts

In order to submit your package on PyPI, you need to have a few accounts on the PyPI website. These accounts will enable you to maintain your packages and will provide you with an interface to edit your package.

Create your account on following sites:

2: The .pypirc file

Now, create a file in your home folder named .pypirc, which will be your configuration file that holds the authentication information of your PyPI accounts.

Create a file .pypirc and put the contents shown below

vim ~/.pypirc

.pypirc file contents

[distutils]
index-servers =
  pypi
  pypitest

[pypi]
repository: https://pypi.python.org/pypi
username: YOUR_USERNAME_HERE
password: YOUR_PASSWORD_HERE

[pypitest]
repository: https://testpypi.python.org/pypi
username: YOUR_USERNAME_HERE
password: YOUR_PASSWORD_HERE

You should replace YOUR_USERNAME_HERE and YOUR_PASSWORD_HERE with your username and password from PyPI sites that you just created.

3: The Python Package directory structure

  • source_dir is a root directory that contains your python package
  • my_python_package is your main python package that you want to publish
source_dir/                 # the source directory
|-- my_python_package       # your package
|   |-- __init__.py
|   `-- FILES ....          # your package files
|-- README.md
|-- setup.cfg
|-- setup.py

Setup your directory structure as shown above, with appropriate changes, and host it on github.com.

This step involves releasing your package on GitHub. This will create a download link of your complete source. In order to release your GitHub project, you need to carry on following steps:

  1. Go to your project homepage on GitHub
  2. On top, you will see Release link. Click on it.
  3. Click on Draft a new relase
  4. Fill in all the details
    • Tag version should be the version number of your package release
    • Release Title can be anything you want.
  5. Click Publish release at the bottom of the page
  6. Now under Releases you can view all of your releases.
  7. Copy the download link (tar.gz) and save it somewhere.

5: Editing files

Open the setup.py file and add following skeleton to it

from distutils.core import setup

setup(
    name = 'my_python_package',
    packages = ['my_python_package'],
    version = 'version number',  # Ideally should be same as your GitHub release tag varsion
    description = 'description',
    author = '',
    author_email = '',
    url = 'github package source url',
    download_url = 'download link you saved',
    keywords = ['tag1', 'tag2'],
    classifiers = [],
)

Open the setup.cfg file and add following skeleton to it

[metadata]
description-file = README.md

Now push everything to GitHub.

6: Publish the package

Execute following commands

python setup.py register -r pypitest

This command will try to register your package on PyPI test server. This makes sure that everything you have setup is correct.

python setup.py sdist upload -r pypitest

This command will upload your package on test repository and now you should see your package on PyPI Test

Now you are ready to publish your package on PyPI Live Server! Execute following commands

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

Congratulations! You just published your python package on PyPI

Reference: Official Documentation

Discover and read more posts from Arpit Bhayani
get started
post commentsBe the first to share your opinion
Saurav Kaushik
6 years ago

Awesome article, Just the URLs in pypirc has now changed to: https://upload.pypi.org/legacy/ and https://test.pypi.org/legacy/

Vasundhara Jalan
7 years ago

When you say, execute the following commands in part 6: Publish the package, where should the commands be executed.
I have made the entire project using PyCharm.
It has been uploaded on GitHub!

Trond Boksasp
7 years ago

Execute the commands on your local machine where you have cloned your github repository. Go to the directory where your setup.py file is located and execute the commands.

Show more replies