Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Managing Python Packages with pip

Recommended resources

In our previous lesson, we established the importance of creating an isolated "workshop" for each of your Python projects using virtual environments. This ensures your projects are self-contained and reproducible. Now that we have our clean workspace, it's time to bring in the tools and materials we need to start building.

Today, we'll focus on pip, Python's package installer. This is the tool you'll use to install external libraries—powerful collections of pre-written code that handle complex tasks for you. We'll learn how to find, install, and manage these libraries, specifically focusing on requests and openai, which will be the cornerstones of the API applications we build throughout this course. Mastering pip is a fundamental step toward writing professional, real-world Python code.

What is pip and PyPI?

Every programming language has an ecosystem of third-party libraries that extend its capabilities, and Python's is one of the largest and most active. These libraries are stored in a central repository called the Python Package Index (PyPI). Think of PyPI as a massive public library for Python code, containing hundreds of thousands of packages for everything from data science to web development and, of course, interacting with APIs.

pip is the tool that acts as your library card. It's the command-line program you use to "check out" (install) packages from PyPI and manage them within your project's virtual environment.

Let's begin by getting a formal introduction to pip and its role in the Python ecosystem.

Using Python's pip to Manage Your Projects' Dependencies

This Real Python article is an excellent, in-depth guide to pip. We'll start with the fundamentals: what pip is and why it's essential to use it within a virtual environment.

Please read the first major section, Getting Started With pip. Pay close attention to the subsection "Using pip in a Python Virtual Environment," as it directly connects to what we learned in the previous lesson. The key takeaway is that when you run pip inside an active venv, you are installing packages only into that isolated project environment.


Installing Your First Packages

With your virtual environment from the last lesson created and activated (you should see (venv) at the start of your terminal prompt), you're ready to install packages. The primary command for this is pip install.

The two most important packages for this course are:

  1. requests: A user-friendly library for making HTTP requests to web servers. We'll use this extensively in the next module to understand how APIs work under the hood.
  2. openai: The official Python client library provided by OpenAI, which simplifies the process of making calls to their LLM APIs.

Let's install them. You can install packages one by one, or you can list multiple packages in a single command. It's common practice to install all necessary packages at once.

In your activated terminal, run the following command:

pip install requests openai

After running the command, pip will connect to PyPI, download the latest versions of requests and openai, and also fetch any other packages they depend on (their "dependencies"). You'll see output in your terminal as the packages are downloaded and installed.

An example of the terminal output after running a `pip install` command. In this case, the packages were already installed, so `pip` simply confirms that the "Requirement already satisfied". In your case, you will see download progress and installation messages.

For a visual demonstration of the pip install command and other core pip functions, the following video by Corey Schafer provides a clear, concise walkthrough.

Python Tutorial: pip - An in-depth look at the package management system

This video is a great tour of the most common pip commands you will use daily.

Please watch the following segments: Installing a package: Shows the basic pip install command. Listing and uninstalling: Demonstrates how to see what's installed with pip list and remove a package with pip uninstall. Checking for updates: Explains how to see outdated packages and upgrade them using the -U flag (pip install -U <package_name>).

As you saw in the video and will see in practice, the main commands for managing packages are quite straightforward:

  • Install: pip install <package_name>
  • List: pip list (shows all packages and their versions in the current environment)
  • Upgrade: pip install --upgrade <package_name> (or -U)
  • Uninstall: pip uninstall <package_name>

Managing Dependencies for Reproducibility: requirements.txt

In the last lesson, we discussed the importance of reproducibility. If you share your project with a colleague, they need a simple way to set up their environment with the exact same dependencies you used. Manually telling them every package to install is inefficient and error-prone.

The standard solution is a requirements file, conventionally named requirements.txt. This file is a simple list of all the packages and their specific versions needed to run the project.

You can generate this file automatically using the pip freeze command. This command "freezes" the current state of your virtual environment's packages and prints it to the terminal. We can then redirect this output into a file.

With your virtual environment still active, run this command:

pip freeze > requirements.txt

Now, if you open your project folder, you'll find a new file named requirements.txt. It will look something like this (your versions may differ):

certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.7
openai==1.17.0
requests==2.31.0
...

This file is the key to a reproducible environment. You should commit requirements.txt to your version control system (like Git). Anyone who checks out your project can then create their own virtual environment, activate it, and run a single command to install all the necessary packages:

pip install -r requirements.txt

The -r flag tells pip to read the packages and versions from the specified file. This single command perfectly recreates the required environment.

This screenshot demonstrates the complete workflow. The first command, `pip freeze > requirements.txt`, creates the file. The second command, `pip install -r requirements.txt`, uses that file to install all the specified packages.

This workflow is a cornerstone of professional Python development. For a more detailed explanation, let's return to the Real Python article.

Using Python's pip to Manage Your Projects' Dependencies

This section covers the requirements.txt workflow in detail, explaining how to pin versions and use the file to replicate an environment.

Please read the entire section Using Requirements Files. It provides a comprehensive look at generating, using, and even fine-tuning these files for development and production.


Conclusion

In this lesson, you've equipped your project "workshop" with the necessary tools. You now know how to use pip, Python's package manager, to install and manage the external libraries that will form the foundation of our applications. This is a critical skill that moves you from writing simple, self-contained scripts to building complex, interconnected systems.

Here are the key takeaways:

  • pip is the command-line tool for installing packages from the Python Package Index (PyPI).
  • Always run pip commands within an activated virtual environment to keep your project dependencies isolated.
  • The core commands are pip install, pip list, pip upgrade, and pip uninstall.
  • A requirements.txt file is the standard way to define and share a project's dependencies.
  • Use pip freeze > requirements.txt to save the state of your environment.
  • Use pip install -r requirements.txt to install all dependencies for a project.

We have now covered the complete setup for a professional Python project: creating an isolated environment with venv and managing dependencies with pip. In the next lesson, we will shift our focus to the code itself. We'll learn how to define and use functions, the fundamental building blocks for organizing your code into logical, reusable units.