Lesson 41 of 70 – PIP
59%

Python PIP

PIP is the standard package-management tool commonly used to install and manage Python packages. It allows developers to install packages, upgrade them, remove them, and manage project dependencies.

Note: PIP is commonly used from the command line or terminal. Modern Python installations often include PIP, although the exact setup depends on how Python was installed.
What is PIP?

PIP is a package installer for Python. It can install packages from package indexes and other supported sources.

For example:

pip install requests

This command asks PIP to install the requests package.

Why Use PIP?
  • Install Python packages.
  • Upgrade installed packages.
  • Uninstall packages.
  • View installed packages.
  • Manage project dependencies.
  • Install packages from supported package indexes and sources.
Check PIP Version

You can check whether PIP is available by displaying its version.

pip --version
Example Output:
pip 25.x.x from ... (python 3.x)

The exact version and installation path depend on your system.

Install a Package

The basic syntax for installing a package is:

pip install package_name

For example:

pip install requests

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

import requests
Install a Specific Version

You can request a particular package version using ==.

pip install requests==2.32.3

The package manager will attempt to install that specified version if it is available and compatible with the environment.

Upgrade a Package

The --upgrade option tells PIP to upgrade a package.

pip install --upgrade requests

PIP checks the available package information and installs a newer compatible version when appropriate.

Uninstall a Package

The uninstall command removes an installed package.

pip uninstall requests

PIP normally asks for confirmation before removing the package.

List Installed Packages

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

pip list
Example:
Package    Version
requests   2.x.x
Show Package Information

The show command displays information about an installed package.

pip show requests

It can display information such as the package version, location and dependencies.

Freeze Installed Packages

The freeze command outputs installed packages in a requirements-style format.

pip freeze

The output can be saved to a file.

pip freeze > requirements.txt
requirements.txt

A requirements.txt file commonly contains the dependencies required by a Python project.

Example:

requests==2.32.3
Flask==3.1.0
numpy==2.2.0

Another environment can install the listed dependencies using:

pip install -r requirements.txt
Install Packages from requirements.txt

The -r option tells PIP to read package requirements from a file.

pip install -r requirements.txt

This is especially useful when setting up an existing Python project on another computer or environment.

Using python -m pip

Instead of calling pip directly, you can run PIP through a specific Python interpreter.

python -m pip --version

For example:

python -m pip install requests

This can be useful when a system has multiple Python installations.

PIP with Python 3

On some systems, the PIP command associated with Python 3 may be named pip3.

pip3 --version

You may also use:

python3 -m pip --version

The exact command depends on the operating system and Python installation.

Search for a Package

Older PIP tutorials sometimes show commands such as:

pip search package_name
Important: The old PyPI XML-RPC search API used by pip search is no longer available. For finding packages, use the Python Package Index website or another package discovery method instead.
Install a Package for the Current User

On systems where you do not have permission to install packages globally, you can use the --user option where supported.

python -m pip install --user requests

This installs the package for the current user rather than system-wide.

PIP and Virtual Environments

PIP is commonly used inside virtual environments. A virtual environment keeps a project's packages separate from packages installed for other projects.

Create a virtual environment:

python -m venv myenv

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

Installing Flask

Flask is a popular Python web framework that can be installed with PIP.

pip install flask

After installation, it can be imported in a Python program:

from flask import Flask
Installing NumPy

NumPy is widely used for numerical and scientific computing.

pip install numpy

Then:

import numpy as np

numbers = np.array([1, 2, 3])

print(numbers)
Output:
[1 2 3]
Installing Multiple Packages

Multiple packages can be installed in one command.

pip install requests flask numpy

PIP resolves and installs the requested packages and their required dependencies.

PIP Dependencies

A package can depend on other packages. These are called dependencies.

When a package declares dependencies, PIP can install the required dependencies as part of the installation process.

pip install some_package

The exact dependencies depend on the package and version being installed.

Upgrade PIP

PIP itself can be upgraded using PIP.

python -m pip install --upgrade pip

Using python -m pip associates the command with the selected Python interpreter.

Common PIP Commands
Command Purpose
pip --version Show PIP version
pip install package Install a package
pip uninstall package Remove a package
pip list List installed packages
pip show package Show package information
pip freeze Show installed packages in requirements format
pip install -r requirements.txt Install requirements from a file
pip install --upgrade package Upgrade a package
PIP Best Practices
  • Use virtual environments for separate projects.
  • Use python -m pip when you want to clearly target a particular Python interpreter.
  • Keep project dependencies documented.
  • Use a requirements.txt file when appropriate.
  • Install packages from trusted sources.
  • Be careful when upgrading dependencies in production projects.
Key Points
  • PIP is commonly used to install and manage Python packages.
  • pip install installs packages.
  • pip uninstall removes packages.
  • pip list displays installed packages.
  • pip show displays package information.
  • pip freeze displays installed packages in requirements format.
  • requirements.txt can document project dependencies.
  • PIP is commonly used inside virtual environments.
  • python -m pip can help target a particular Python interpreter.

🧠 Quick Quiz

Question: Which command is commonly used to install a Python package?