Chapter 3. Python Packages

In this chapter, you’ll learn how to package your Python projects for distribution. A package is a single file containing an archive of your code along with metadata that describes it, like the project name and version.

Note

Python folks use the word package for two distinct concepts. Import packages are modules that contain other modules. Distribution packages are archive files for distributing Python software—​they’re the subject of this chapter.

You can install a package into a Python environment using a package installer like pip. You can also upload it to a package repository for the benefit of others. The Python Software Foundation (PSF) operates a package repository known as the Python Package Index (PyPI). If your package is on PyPI, anyone can install it by passing its project name to pip install.

Packaging your project makes it easy to share with others, but there’s another benefit. When you install your package, it becomes a first-class citizen of a Python environment:

  • The interpreter imports your modules from the environment—​rather than an arbitrary directory on your filesystem, which may or may not work depending on how you invoke Python.

  • Installers use the package metadata to ensure the environment matches the prerequisites of your package, such as the minimum Python version and any third-party packages it depends on.

  • Installers can generate entry-point scripts that ensure your code always runs on the interpreter in the environment. ...

Get Hypermodern Python Tooling now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.