5) Virtual Environments and Packages Lesson

Python Libraries, Packages and Modules

4 min to complete · By Martin Breuss

In the lesson about type hinting in Python, you've learned about an external package called mypy. If you try to run the mypy command without first installing it, you'll encounter a new error friend. If you went to check it out on GitHub and read over their installation instructions, then you've probably come across the line:

python3 -m pip install mypy

While Python already comes with a lot of code in its standard library, there are a vast amount of additional packages available that you can install from external sources. mypy is one such package.

The Standard Library

Python boasts to come with "batteries included", which means, according to Python's PEP 206:

having a rich and versatile standard library that is immediately available, without making the user download separate packages.

The standard library contains a large number of extremely useful packages that are often enough to tackle most tasks you want to accomplish with Python.

You've used packages from the standard library before when importing pathlib and random.

However, another reason that Python is so widely used is its rich ecosystem of packages and modules. It also helps that it's straightforward to install and use an external codebase.

Third-Party Packages

The Python Package Index (PyPI) is a central repository where all Python users can upload their own code as a package and where everyone can go to download those packages.

PyPI logo

PyPI contains the most well-known third-party packages that you might want to work with. Because so many packages are hosted in this central place, there's also a common tool that comes with your Python installation that allows you to quickly install a package from PyPI. This tool is called pip.

Python Pip Install Packages

Python ships with a tool to install packages called pip. pip stands for "pip installs packages" and it makes installing modules and packages quite straightforward:

python3 -m pip install <package name>

For example, in order to install mypy locally on your computer, you only need to type this one line of code into your terminal:

python3 -m pip install mypy

This command would install mypy system-wide for your python3 installation.

Illustration of a lighthouse

Note: You most likely don't want to do that. Installing external packages system-wide is a bad practice because it can lead to version clashes down the line.

However, instead of installing your packages system-wide, you're better off installing them in a virtual environment. You'll learn more about these in the upcoming lessons.

Colorful illustration of a light bulb

Additional Resources

  • Python Packaging Index Website: PyPI
  • Wikipedia: pip

Summary: Python Packages and Modules

  • Python comes with a lot of great packages, and you can install even more of them from a central location called PyPI with the pre-installed package manager pip.
  • Avoid installing packages system-wide, so keep reading before you go crazy and pollute your system with external packages.