10.1. Know Where to Find Community-Built Modules

Python has a central repository of modules (https://pypi.org) that you can install and use in your programs. These modules are built and maintained by people like you: the Python community. When you find yourself facing an unfamiliar challenge, the Python Package Index (PyPI) is a great place to look for code that will get you closer to your goal.

To use the Package Index, you need to use the command-line tool pip (a recursive acronym for “pip installs packages”). pip can be run with python3 -m pip to ensure that packages are installed for the correct version of Python on your system (see Item 1: “Know Which Version of Python You’re Using”). Using pip to install a new module is simple. For example, here I install the pytz module that I use elsewhere in this book (see Item 67: “Use datetime Instead of time for Local Clocks”):

$ python3 -m pip install pytz Collecting pytz

Downloading ...

Installing collected packages: pytz Successfully installed pytz-2018.9

pip is best used together with the built-in module venv to consistently track sets of packages to install for your projects (see Item 83: “Use Virtual Environments for Isolated and Reproducible Dependencies”). You can also create your own PyPI packages to share with the Python community or host your own private package repositories for use with pip.

Each module in the PyPI has its own software license. Most of the packages, especially the popular ones, have free or open source licenses (see https://opensource.org for details). In most cases, these licenses allow you to include a copy of the module with your program; when in doubt, talk to a lawyer.

10.1.1. Things to Remember

✦ The Python Package Index (PyPI) contains a wealth of common packages that are built and maintained by the Python community.

✦ pip is the command-line tool you can use to install packages from PyPI.

✦ The majority of PyPI modules are free and open source software.