Lesson 56 · Python Environment & Tools 40–50 minutes Intermediate

Python pip

Learn how to install, upgrade, remove and manage Python packages using pip — one of the most important tools in the Python ecosystem.

0%
01

Learning Objectives

In this lesson, you will learn how Python packages are installed and managed using pip.

You will learn how to:

  • Understand what pip is.
  • Understand Python packages.
  • Check whether pip is installed.
  • Install Python packages.
  • Install specific package versions.
  • Upgrade and uninstall packages.
  • Inspect installed packages.
  • Use requirements.txt.
  • Understand pip freeze.
  • Use pip inside virtual environments.
  • Understand common pip problems.
02

What Is pip?

pip is Python's standard package installer. It allows you to install and manage packages from the Python Package Index and other package sources.

Python itself provides many built-in modules, but developers often need additional functionality. pip makes installing those external packages much easier.

Simple Idea

Think of pip as a package manager for Python. It helps you obtain the libraries your projects need.

python -m pip install requests

This command tells pip to install the requests package.

03

Why Use pip?

Modern Python applications often depend on many third-party packages.

For example, a data-analysis application might use packages such as NumPy, Pandas and Matplotlib.

A web application might use Flask or FastAPI.

Instead of manually downloading and configuring every library, pip provides a consistent way to install them.

Typical Workflow

  1. Choose a package.
  2. Install it with pip.
  3. Import it in Python.
  4. Use its functionality.
04

Python Packages

A package is a collection of Python code that can be installed and reused in your projects.

Popular packages provide functionality for:

  • Data analysis
  • Machine learning
  • Web development
  • HTTP requests
  • Data visualization
  • Database access
  • Automation

For example, after installing requests, you can write:

import requests

response = requests.get("https://example.com")

print(response.status_code)

The package provides functionality that is not part of Python's basic language syntax.

05

Checking Whether pip Is Installed

You can check pip from your terminal or command prompt.

python -m pip --version

On some systems, the Python command may be python3.

python3 -m pip --version

On Windows, the Python launcher can also be used:

py -m pip --version

Why python -m pip?

It explicitly runs pip through the Python interpreter you selected, which can help avoid confusion when multiple Python installations exist.

06

Installing Packages

The basic installation command is:

python -m pip install package_name

For example:

python -m pip install requests

After installation, the package can be imported into your Python program.

import requests

print("Requests is ready!")

Important

Installation and importing are two separate operations. pip installs the package; Python's import statement makes its code available to your program.

07

Installing a Specific Version

Sometimes your project requires a particular version of a package.

Use the == operator:

python -m pip install requests==2.32.3

You can also specify version ranges.

python -m pip install "requests>=2.30,<3.0"

Version constraints become particularly important in professional projects where dependency compatibility matters.

08

Upgrading Packages

To upgrade an installed package, use --upgrade.

python -m pip install --upgrade requests

Short form:

python -m pip install -U requests

Be Careful with Upgrades

Updating packages can sometimes introduce compatibility changes. In production projects, dependencies should be upgraded deliberately and tested.

09

Uninstalling Packages

If a package is no longer required, you can remove it using uninstall.

python -m pip uninstall requests

pip normally asks for confirmation before removing the package.

Example

Proceed (Y/n)?

Enter Y if you want to continue.

10

Listing Installed Packages

Use pip list to display packages installed in the current Python environment.

python -m pip list

You will typically see package names and their installed versions.

Example Output
Package      Version
------------ -------
pip          25.x
requests     2.x
urllib3      2.x

The exact versions depend on your environment.

11

Inspecting a Package

Use pip show to inspect information about an installed package.

python -m pip show requests

Information may include:

  • Package name
  • Version
  • Summary
  • Installation location
  • Dependencies

This can be useful when troubleshooting package installations.

13

requirements.txt

A Python project often needs multiple external packages.

Instead of manually recording every package, projects commonly use a file called requirements.txt.

requests==2.32.3
pandas==2.2.3
numpy==2.1.3
matplotlib==3.9.2

The file describes dependencies required by the project.

Why Is This Useful?

Another developer can install the project's dependencies instead of manually figuring out which packages are required.

14

Installing Requirements

If a project contains a requirements.txt file, you can install all listed dependencies with:

python -m pip install -r requirements.txt

The -r option tells pip to read package requirements from the specified file.

Typical Project Setup

project/
│
├── app.py
├── requirements.txt
└── README.md
15

pip freeze

The pip freeze command displays installed packages in a format suitable for a requirements file.

python -m pip freeze

You can save the result into a file:

python -m pip freeze > requirements.txt

The > operator redirects the command output into the file.

Important Distinction

pip list is designed for a readable listing, while pip freeze produces pinned package-style output commonly used for dependency files.

16

Why Use python -m pip?

You may see commands written in two different ways:

pip install requests

and:

python -m pip install requests

Both can invoke pip, but python -m pip explicitly connects pip with the Python interpreter represented by python.

Professional Habit

Using python -m pip can reduce confusion when multiple Python installations or environments exist on your machine.

17

Upgrading pip

pip itself is also a Python package and can be upgraded.

python -m pip install --upgrade pip

After upgrading, verify the installed version:

python -m pip --version

Best Practice

Upgrade tools deliberately, especially in professional environments where reproducibility is important.

18

pip and Virtual Environments

One of the most important concepts in professional Python development is the use of virtual environments.

A virtual environment creates an isolated Python environment for a project.

This means different projects can use different package versions without interfering with each other.

python -m venv .venv

After activating the environment, pip installs packages into that environment.

Think About It This Way

Project A might need one version of a package, while Project B needs another. Virtual environments keep those dependencies isolated.

We will study virtual environments in much more detail in the next dedicated lesson.

19

Common pip Mistakes

1. Using the Wrong Python Environment

You may install a package successfully but still receive an import error because your program is running with a different Python interpreter.

2. Installing Globally Without Thinking

Installing everything globally can create dependency conflicts between projects.

Virtual environments are generally a better approach for project dependencies.

3. Ignoring Version Compatibility

A package version may not support your Python version or may conflict with another dependency.

4. Running pip from the Wrong Directory

When installing from requirements.txt, make sure the file path is correct.

5. Trusting Every Package

External packages become part of your software supply chain. Check their reputation, documentation, maintenance and security before using them.

Remember

Installing a package gives code from an external source access to your Python environment. Choose dependencies carefully.

20

Practice Exercises

Exercise 1 — Check pip

Open your terminal and determine the installed pip version.

python -m pip --version

Exercise 2 — Install a Package

Install the requests package and verify that it appears in your installed packages.

Exercise 3 — Inspect a Package

Use pip show to inspect the requests package.

Exercise 4 — Create Requirements

Generate a requirements file from your current environment using pip freeze.

Exercise 5 — Read Requirements

Create a small requirements.txt file containing three package requirements and install them using -r.

21

Mini Challenge — Prepare a Python Project

Imagine that you are preparing a small data-analysis project for another developer.

Your Mission

Prepare the project's Python environment using pip.

  1. Create a new project folder.
  2. Create a virtual environment called .venv.
  3. Activate the environment.
  4. Install the packages needed by your project.
  5. Verify the installed packages.
  6. Create a requirements.txt file.
  7. Use that file to reproduce the environment.

Suggested Dependencies

pandas
numpy
matplotlib

Suggested Commands

python -m venv .venv

python -m pip install pandas numpy matplotlib

python -m pip list

python -m pip freeze > requirements.txt

Challenge Extension

Delete the environment after completing your experiment, recreate it and use requirements.txt to reinstall the dependencies.

This simulates an important real-world workflow: rebuilding a project's environment from its dependency file.

22

Knowledge Check

1. What is pip?

2. Which command installs a package?

3. Which command displays installed packages?

4. What does pip freeze commonly help create?

5. Which option installs from a requirements file?

6. Which command upgrades a package?

7. Why are virtual environments useful?

8. What does python -m pip show provide?

23

Lesson Summary

pip is one of the most important tools in the Python ecosystem because it allows developers to install and manage external packages.

What You Learned

  • pip is Python's package installer.
  • Packages provide reusable functionality.
  • python -m pip install installs packages.
  • Specific versions can be installed using version specifiers.
  • --upgrade updates packages.
  • uninstall removes packages.
  • pip list displays installed packages.
  • pip show displays package information.
  • requirements.txt records project dependencies.
  • pip freeze can produce dependency information suitable for a requirements file.
  • python -m pip helps associate pip with a specific Python interpreter.
  • Virtual environments isolate project dependencies.

You now have an important piece of professional Python workflow knowledge: installing and managing the libraries that your applications depend on.

In the next lesson, we will go deeper into Virtual Environments and learn how to create isolated Python environments for individual projects.

Ready to Continue?

You have completed the Python pip lesson. Mark this lesson as complete when you're ready for the next topic.