Chapter 17: The Python Ecosystem - Installing Packages with pip

In the last chapter, you explored the Python Standard Library, a powerful set of tools that comes built-in with Python. But what happens when you need to do something that isn't covered by the standard library, like make HTTP requests to a web API, process images, or perform complex data analysis?

This is where the Python Package Index (PyPI) comes in. PyPI (pronounced "pie-P-eye") is a massive repository of software for the Python programming language, created and shared by the community. It contains hundreds of thousands of third-party packages that you can install and use in your own projects.

To install these packages, you use a command-line tool called pip.

What is pip?

pip is Python's official package installer. When you installed Python (as we discussed in Chapter 2), pip was very likely installed along with it. It allows you to find, download, and install packages from PyPI with simple commands.

How to Use pip

You will use pip from your computer's command line or terminal.

  • On Windows, this is typically the Command Prompt or PowerShell.

  • On macOS and Linux, this is the Terminal app.

The basic command structure is pip install <package_name>.

Let's Install a Package: requests

The requests library is one of the most popular Python packages. It makes sending HTTP requests to web servers incredibly simple, which is useful for things like getting data from a website's API.

To install it, open your terminal and type the following command, then press Enter:

pip install requests

You should see pip connect to the internet, download the package, and install it.

Note: Depending on your system's configuration, you might need to use pip3 instead of pip, or python -m pip if you have multiple Python versions installed. For example: pip3 install requests.

Using an Installed Package

Once a package is installed, you use it in your code just like you would a standard library module: with the import statement.

Let's write a small program to get information about a random Pokémon using a free public API called the PokéAPI.

Run this code. Each time you run it, you'll connect to the API and get a different Pokémon! This simple program would have been much more complicated without the requests library.

Managing Packages

Here are a few other pip commands you should know:

  • List installed packages: pip list

  • Uninstall a package: pip uninstall requests

  • Upgrade a package: pip install --upgrade requests

Summary and What's Next

You've now learned how to tap into the global Python community's incredible collection of open-source software.

  • PyPI is the Python Package Index, where third-party packages are hosted.

  • pip is the command-line tool used to install and manage these packages.

  • The pip install package_name command is used to download and install a package.

  • Once installed, you can import a third-party package just like a standard library module.

You have come a long way and now have all the fundamental skills to build interesting and powerful Python applications. The final chapter of this book will serve as a conclusion, offering guidance on where to go from here to continue your Python journey.

Practice Time!

  1. Think of a topic you're interested in (space, movies, weather, etc.).

  2. Search online for a "free public API" for that topic.

  3. Write a simple program using the requests library to connect to that API and print out some interesting data you find.

Last updated