0:0040:25

How to create and publish a Python package

Irina Truong 40:25 2020

Publishing a Python package takes three tools, two commands and no magic

A one-file script that prints a random daily message becomes a command anyone in the world can install with pip. Skip a step and users face slow source builds, or a missing license leaves them no permission to use the code at all. Backend engineer Irina Truong of Parse.ly asks what really stands between a working script and a package strangers can install.

Follow the full path from main.py to a pip-installable package on PyPI, with a live demo

Watch on YouTube

In short

Irina Truong, backend engineer at Parse.ly, walks through turning a single Python script into a pip-installable console application published on PyPI. She covers package structure, setup.py metadata and entry points, source and wheel distributions, safe uploading to TestPyPI with twine and API tokens, and choosing a license, arguing that the whole process involves no magic.

“And there is no magic whatsoever in the whole process.”
“Because when you include no license, by default, it means people don't have any permissions to do anything with your code. They can't copy, distribute or modify it.”

What you will learn

  • Structure a Python module as a package with a package directory, __init__.py, setup.py and a README.
  • Configure setup.py metadata, classifiers and console entry points so users can run your tool as a shell command.
  • Build both source and wheel distributions and explain why wheels install faster, especially for native extensions.
  • Publish safely by testing on TestPyPI with an API token stored in .pypirc and uploading with twine.
  • Choose an open source license such as MIT or GNU GPL v3 and add a changelog, contributing guide and requirements files.

Details

Level
Beginner
Format
Tech talk
Technical depth
Practitioner
For
Student, Early career, Mid-career
Length
40:25
Published
July 2020

Questions this talk answers

What is the difference between a Python library and a Python application?

Libraries are installable, importable and versioned and target developers. Applications have the same features but are also runnable and target end users.

Why build both a source distribution and a wheel?

A source distribution is the older format and must be built on the user's system, while a wheel has no build step and installs faster. The difference is largest for packages with native extensions such as NumPy or SciPy, and pip uses a wheel by default when one is available.

Why use an API token instead of a username and password when uploading?

The credentials are stored in a plain text .pypirc file in your home directory, so it is safer to store a token there than your login and password.

What happens if I publish an open source package with no license?

With no license, people by default have no permission to copy, distribute or modify your code. She suggests choosealicense.com to pick one that fits.

Where do developers usually run into friction when publishing?

Common problems are building only a source distribution and not a wheel, naming a package the same as an existing one, and forgetting to update the version before re-uploading.

About this talk

Once you start writing Python code, there comes a moment when your code becomes useful enough, mature enough, and/or unique enough to share with the world. So where do you start, and what are the best practices for packaging and sharing your code? In this workshop, we're going to walk through creating and publishing a package, talk about development workflows, licensing, and answer various questions that might come up once you set out on this path.

If you want to follow along and create a package, you will need: * a computer with wifi / internet access * Python interpreter available (3.6+ recommended) * basic knowledge of Python.

Topics

Related talks

Browse the library