Publishing a Python Package to the Python Package Index (PyPI)

Published: 2019-05-30

By: MJ Rossetti

Category:
Technologies:

Repository

Project Page

This document describes the process of building and releasing a Python Package to the Python Package Index (PyPI).

The first Python package I released contains logic to play a game of “Rock-Paper-Scissors”.

After a package is released to the PyPI, we can use pip to install it:

pip install s2t2-game-utils

So we can import and use it in other programs:

from game_utils.rock_paper_scissors import determine_winner

determine_winner("rock", "paper") #> "paper"

Repo Structure

Here is the structure of the Python package’s repository:

my-repo/
│
├── game_utils/
│   ├── __init__.py
│   └── rock_paper_scissors.py
│
├── test/
│   ├── rock_paper_scissors_test.py
│
├── conftest.py
├── LICENSE.md
├── README.md
└── setup.py

And importantly, the contents of “setup.py” (which will be used to configure the build):

# setup.py

from setuptools import find_packages, setup

with open("README.md", "r") as fh:
    long_description = fh.read()

setup(
    name="s2t2-game-utils",
    version="1.0",
    author="MJ Rossetti",
    author_email="datacreativellc@gmail.com",
    description="Gameplay logic for Rock-Paper-Scissors",
    long_description=long_description,
    long_description_content_type="text/markdown", # required if using a md file for long desc
    license="MIT",
    url="https://github.com/s2t2/game-utils-py",
    keywords="rock paper scissors game",
    packages=find_packages() # ["game_utils"]
)

Build and Release Process

Prerequisites

Setup

Create and activate a virtual environment:

conda create -n twine-env python=3.7 # (first time only)
conda activate twine-env

pip install twine # (first time only)

Building

Generate / build package distribution files:

python setup.py sdist bdist_wheel

Check the distribution to ensure the “setup.py” file has been configured properly:

twine check dist/*

If there is an error in the distribution, you’ll need to re-generate it and re-check it.

Releasing

Otherwise, push to the PYPI Test Server:

twine upload --skip-existing --repository-url https://test.pypi.org/legacy/ dist/*

Finally, if everything looks good, release to PyPI:

twine upload --skip-existing dist/*

Oh yeah :-D

A screenshot of the package on the python package index