1. Introduction#


Python packages are a core element of the Python programming language and are how you write reusable and shareable code in Python. This book assumes that readers are familiar with how to install a package using a package installer like pip or conda, and how to import and use it with the help of the import statement in Python.

For example, the command below uses pip to install numpy5, the core scientific computing package for Python:

pip install numpy

Once the package is installed, it can be used in a Python interpreter. For example, to round pi to three decimal places:

python
import numpy as np
np.round(np.pi, decimals=3)
3.142

At a minimum, a package bundles together code (such as functions, classes, variables, or scripts) so that it can be easily reused across different projects. However, packages are typically also supported by extra content such as documentation and tests, which become exponentially more important if you wish to share your package with others.

As of January 2022, there are over 350,000 packages available on the Python Package Index (PyPI), the official online software repository for Python. Packages are a key reason why Python is such a powerful and widely used programming language. The chances are that someone has already solved a problem that you’re working on, and you can benefit from their work by downloading and installing their package. Put simply, packages are how you make it as easy as possible to use, maintain, share, and collaborate on Python code with others, whether they be your friends, work colleagues, the world, or your future self!

Even if you never intend to share your code with others, making packages will ultimately save you time. Packages make it significantly easier for you to reuse and maintain your code within a project and across different projects. After programming for some time, most people will eventually reach a point where they want to reuse code from one project in another. For beginners, in particular, this is something often accomplished by copying-and-pasting existing code into the new project. Despite being inefficient, this practice also makes it difficult to improve and maintain your code across projects. Creating a simple Python package will solve these problems.

Regardless of your motivation, the goal of this book is to show you how to easily develop Python packages. The focus is overwhelmingly practical — we will leverage modern methods and tools to develop and maintain packages efficiently, reproducibly, and with as much automation as possible, so you can focus on writing and sharing code. Along the way, we’ll also enlighten some interesting and relevant lower-level details of Python packaging and the Python programming language.

1.1. Why you should create packages#

There are many reasons why you should develop Python packages!

  • To effectively share your code with others.

  • They save you time. Even if you don’t intend to share your package with others, they help you easily reuse and maintain your code across multiple projects.

  • They force you to organize and document your code, such that it can be easily understood and used at a later time.

  • They isolate dependencies for your code and improve its reproducibility.

  • They are a good way to practice writing good code.

  • Packages can be used to effectively bundle up reproducible data analysis and programming projects.

  • Finally, developing and distributing packages supports the Python ecosystem and other Python users who can benefit from your work.