pip - Package Installer

Learn how to use pip, Python's package installer, to install, manage, and work with third-party packages.

Ali Berro

By Ali Berro

5 min read Section 2
From: Python Fundamentals: From Zero to Hero

pip - Package Installer

pip (Pip Installs Packages) is Python’s standard package manager. It allows you to install and manage third-party packages from the Python Package Index (PyPI) and other sources.

What is pip?

pip is a command-line tool that comes with Python (version 3.4+) and allows you to:

  • Install packages from PyPI
  • Uninstall packages
  • List installed packages
  • Show package information
  • Search for packages
  • Manage package versions

Checking pip Installation

First, verify that pip is installed:

Terminal window
pip --version
# or
pip3 --version

If pip is not installed, you can install it using:

Terminal window
python -m ensurepip --upgrade

Basic pip Commands

Install a Package

Terminal window
pip install package_name

Example:

Terminal window
pip install requests
pip install numpy pandas matplotlib

Install Specific Version

Terminal window
pip install package_name==version
pip install requests==2.28.0

Install from Version Range

Terminal window
pip install "package_name>=2.0,<3.0"
pip install "requests>=2.25.0,<3.0.0"

Upgrade a Package

Terminal window
pip install --upgrade package_name
# or
pip install -U package_name

Uninstall a Package

Terminal window
pip uninstall package_name

List Installed Packages

Terminal window
pip list

Show Package Information

Terminal window
pip show package_name

Search for Packages

Terminal window
pip search query

Note

The pip search command has been disabled on PyPI. Use the web interface at https://pypi.org/ to search for packages.

Installing from Requirements File

Create a requirements.txt file:

requirements.txt
requests==2.28.0
numpy>=1.20.0
pandas==1.5.0
matplotlib

Install all packages from the file:

Terminal window
pip install -r requirements.txt

Common pip Options

Install to User Directory

Terminal window
pip install --user package_name

Install in Development Mode

Terminal window
pip install -e .

Ignore Installed Packages

Terminal window
pip install --ignore-installed package_name

No Cache

Terminal window
pip install --no-cache-dir package_name

Verbose Output

Terminal window
pip install -v package_name

Here are some commonly used packages:

Data Science

  • NumPy: Numerical computing
  • Pandas: Data manipulation and analysis
  • Matplotlib: Plotting and visualization
  • SciPy: Scientific computing

Web Development

  • Flask: Lightweight web framework
  • Django: Full-featured web framework
  • Requests: HTTP library

Machine Learning

  • scikit-learn: Machine learning library
  • TensorFlow: Deep learning framework
  • PyTorch: Deep learning framework

Utilities

  • Pillow: Image processing
  • BeautifulSoup4: HTML/XML parsing
  • python-dotenv: Environment variables

Installing Packages

Example: Installing NumPy

Terminal window
pip install numpy

Then use it in Python:

using-numpy.py
import numpy as np
# Create an array
arr = np.array([1, 2, 3, 4, 5])
print(arr) # [1 2 3 4 5]
# Perform operations
print(arr.mean()) # 3.0
print(arr.sum()) # 15

Example: Installing Requests

Terminal window
pip install requests
using-requests.py
import requests
# Make HTTP request
response = requests.get('https://api.github.com')
print(response.status_code) # 200

Managing Package Versions

Check Installed Version

Terminal window
pip show package_name

Install Latest Version

Terminal window
pip install --upgrade package_name

Install Specific Version

Terminal window
pip install package_name==2.1.0

Downgrade Package

Terminal window
pip install package_name==1.9.0

Requirements Files

Creating requirements.txt

Generate a requirements file from installed packages:

Terminal window
pip freeze > requirements.txt

This creates a file with all installed packages and their versions:

requirements.txt
numpy==1.24.0
pandas==1.5.3
matplotlib==3.6.2
requests==2.28.0

Installing from requirements.txt

Terminal window
pip install -r requirements.txt

Virtual Environments and pip

It’s recommended to use pip within virtual environments:

Terminal window
# Create virtual environment
python -m venv myenv
# Activate (Windows)
myenv\Scripts\activate
# Activate (Linux/Mac)
source myenv/bin/activate
# Install packages
pip install package_name

Troubleshooting pip

Upgrade pip

Terminal window
python -m pip install --upgrade pip

Clear pip Cache

Terminal window
pip cache purge

Check pip Configuration

Terminal window
pip config list

Install from Alternative Source

Terminal window
pip install -i https://pypi.org/simple/ package_name

pip Best Practices

  1. Use virtual environments: Isolate project dependencies
  2. Pin versions: Use requirements.txt with specific versions
  3. Keep pip updated: Regularly upgrade pip itself
  4. Use requirements files: Track all dependencies
  5. Test after installation: Verify packages work correctly

Exercises

Exercise 1: Understanding pip Commands

Write the pip command to install the requests package. Then write code to import requests and make a simple GET request to ‘https://httpbin.org/get’, printing the status code.

Understanding pip Commands

Checks: 0 times
Answer:
# Command: pip install requests
import requests
response = requests.get('https://httpbin.org/get')
print(response.status_code)

Exercise 2: Requirements File

Create a requirements.txt content with numpy version 1.24.0 and pandas version 1.5.3. Then write the pip command to install from this file.

Requirements File

Checks: 0 times
Answer:
# requirements.txt content:
# numpy==1.24.0
# pandas==1.5.3
# Command: pip install -r requirements.txt
print("requirements.txt created")
print("Command: pip install -r requirements.txt")

Course Progress

Section 57 of 61

Back to Course