Skip to content

Python Packaging - an Overview

Overview: Python packaging#

Python packaging is extremely broad. It has a long history and isn’t easily learned in a day. You learn it by packaging something, and by then finding out which of the caveats applies to you.

This page is the judgement part: which format, what a wheel buys you, why an install needs a compiler, and the shortest safe path to a first release. The step by step lives in the packaging docs.

Wheel vs Egg#

Both are built artifacts that install without compiling on the user’s machine.

  • The egg was introduced by setuptools in 2004, with no PEP behind it
  • The wheel was introduced by PEP 427 in 2012 and is the standard

The difference that matters:

  • An egg doubles as a runtime format. Code is imported from the zip, so it cannot be inspected by an IDE, and it may ship .pyc files built for another Python version
  • A wheel is a distribution format only. It never contains .pyc, it uses a .dist-info folder per PEP 376 where the egg used .egg-info, and its file name encodes the Python version, ABI and platform it is good for

Use pip install wheel then python -m build (or setup.py bdist_wheel in old projects) and ship wheels. Eggs are history.

Native Extensions and the Compiler Tax#

If a wheel exists for your platform, pip install cryptography takes seconds. If it does not, pip falls back to the source distribution and you get to be a C developer for the afternoon:

error: command 'gcc' failed
fatal error: Python.h: No such file or directory

The usual sequence on Debian or Ubuntu:

apt install python3-dev libffi-dev libssl-dev

Then it compiles, and now every machine and every CI run has to do the same. That is the whole argument for wheels, and the reason an extension wheel is worth publishing even when the sdist is “portable”.

Conda tried to solve this by shipping binaries outside PyPI. It never got a PEP, it cannot publish to PyPI, and its packages do not work in a plain virtual environment, which is why it stayed a separate ecosystem rather than replacing wheels.

Command Line Scripts#

How do you package a command line tool so it looks like a binary on the PATH. There are two mechanisms.

Scripts#

Write the script as a file, put it in mypackage/bin, and list it:

setup(
    ...
    scripts=['bin/funniest-joke'],
    ...
)

pip copies it onto the path and rewrites the shebang. It does not have to be Python, which is the one advantage.

Console Scripts (entrypoints)#

Entrypoints register a Python function, not a file:

# mypackage/command_line.py
import funniest

def main():
    print(funniest.joke())

setup(
    ...
    entry_points={
        'console_scripts': ['funniest-joke=funniest.command_line:main'],
    }
    ...
)

Setuptools generates a small shim that imports the module and calls the function. Because the entrypoint is a normal function, you can test it by importing it and calling it, instead of shelling out to a subprocess:

from mypackage.command_line import main

def test_basic():
    main()

That testability is why entrypoints are the better default.

Editable Installs#

pip install . copies the package into the virtual environment’s site-packages, so every edit needs a reinstall. For development use:

pip install -e .

Editable mode leaves the files where they are and installs a link to the source directory, so an edit is live on the next import. This is preferred over the legacy python setup.py install, which also skips pip’s dependency handling and its uninstall record.

Publishing to PyPI#

The shortest path that is not embarrassing:

  1. setup.py (or pyproject.toml) with name, version, description and packages=find_packages(exclude=['docs', 'tests'])
  2. A virtual environment, pip install -e ., then import the package and call it, so you know the packaging is right before uploading
  3. An account on test.pypi.org and a ~/.pypirc with both indexes
  4. pip install build twine
  5. python -m build then twine upload --repository testpypi dist/*
  6. Fill in what the metadata check complains about: url, author, author_email, python_requires, install_requires, long_description with long_description_content_type, and a license
  7. Classifiers are cosmetic, but they are what the PyPI search and filters use

Once a second person or a second package exists, automate it. Credentials in one person’s home directory do not scale, and a release that is a sequence of manual commands is a release that eventually goes out half done. tox or nox for the matrix, a CI job for the upload with a token, and a version bump that happens in one place.

Source#