Tutorials Python Python pip & Packages
Ch 30 / 58
Python Intermediate 12 min read Chapter 30 of 58

Python pip & Packages

pip (Pip Installs Packages) is Python's official package manager — your gateway to the Python Package Index (PyPI), which hosts over 500,000 open-source libraries. With a single command you can add powerful capabilities to your projects: data analysis with pandas, machine learning with scikit-learn, web frameworks like Django, and thousands more. This chapter covers every pip command, how to create isolated virtual environments so projects don't conflict, requirements.txt for reproducible setups, and the most important packages every Python developer should know.

What you'll learn in this chapter
What pip is and how PyPI works
Installing, upgrading and uninstalling packages
Listing, searching and inspecting installed packages
Virtual environments with venv
requirements.txt — reproducible installs
Popular packages every Python dev should know
Advertisement

What is pip and PyPI?

pip is Python's built-in package manager — a command-line tool that downloads, installs, and manages third-party packages. PyPI (the Python Package Index at pypi.org) is the central repository hosting 500,000+ packages. When you run pip install requests, pip fetches the package from PyPI and installs it into your Python environment.

pip Ecosystem — How Package Installation Works
PyPI
pypi.org
500k+ packages
→
pip install
package manager
downloads & installs
→
venv
isolated env
project-specific
→
Your Project
import & use
ready to use
Terminal / Command Prompt
$ python --version
Python 3.12.0

$ pip --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)

# pip comes bundled with Python 3.4+ — no installation needed
# If pip is missing, install it:
$ python -m ensurepip --upgrade

# Upgrade pip to the latest version
$ python -m pip install --upgrade pip
pip vs pip3 On systems with both Python 2 and Python 3, use pip3 to ensure you're installing for Python 3. On modern systems (Python 3 only), pip and pip3 are equivalent. The safest form is always python -m pip ....

Installing Packages

The most common pip command is pip install. You can install a package, a specific version, a minimum version, or multiple packages at once. pip resolves and installs all dependencies automatically.

Terminal — pip install commands
# Install the latest version
$ pip install requests

# Install a specific version
$ pip install requests==2.31.0

# Install minimum version
$ pip install "requests>=2.28"

# Install within a version range
$ pip install "django>=4.0,<5.0"< /span>

# Install multiple packages at once
$ pip install requests flask pandas

# Upgrade an already-installed package
$ pip install --upgrade requests
Successfully installed requests-2.31.0

# Install from requirements.txt
$ pip install -r requirements.txt

# Install in editable/development mode (for your own packages)
$ pip install -e .
Python — using_installed_package.py
# After: pip install requests
import requests

response = requests.get("https://api.github.com")
print(response.status_code)     # 200
print(response.json()["current_user_url"])

# After: pip install pandas
import pandas as pd

df = pd.DataFrame({
    "name":  ["Alice", "Bob", "Carol"],
    "score": [92, 85, 88]
})
print(df)
print(df["score"].mean())          # 88.33

Managing Installed Packages

Beyond installing, pip lets you inspect what's installed, search for packages, uninstall packages, and check for outdated versions. These commands are your day-to-day package maintenance toolkit.

Terminal — pip management commands
# List all installed packages
$ pip list
Package Version
---------- -------
requests 2.31.0
pandas 2.1.4

# Show details of a specific package
$ pip show requests
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Requires: certifi, charset-normalizer, idna, urllib3

# Check for outdated packages
$ pip list --outdated

# Uninstall a package
$ pip uninstall requests
Found existing installation: requests 2.31.0
Uninstalling requests-2.31.0: Proceed (y/n)? y
Successfully uninstalled requests-2.31.0

# Uninstall without prompt
$ pip uninstall -y requests

# Search PyPI (use browser — pip search is deprecated)
# Visit: https://pypi.org/search/?q=your-query
Advertisement
K2infocom Python Masterclass & Backend Development
Master Python, Django & Backend APIs with AI
Go from absolute beginner to building production-ready REST APIs, web applications, and ML pipelines. Real projects, live mentoring in Vadodara, and placement support included.
2,400+
Students Enrolled
4.9 ⭐
Google Rating
6 Months
Full Program
100%
Placement Support
Python 3 Django & DRF FastAPI PostgreSQL Machine Learning Docker & AWS Real Projects AI Tools

Virtual Environments with venv

A virtual environment is an isolated Python environment with its own installed packages — completely separate from your system Python and other projects. This prevents version conflicts: Project A can use Django 4.2 while Project B uses Django 5.0, with no interference. Always use a virtual environment for every project.

Golden rule — one venv per project Never install project packages globally (outside a venv). Always create a venv first, activate it, then install. This keeps projects independent and reproducible.
Terminal — virtual environment workflow
# Step 1: Create a virtual environment named 'venv'
$ python -m venv venv

# Step 2: Activate the virtual environment
# macOS / Linux:
$ source venv/bin/activate
# Windows Command Prompt:
> venv\Scripts\activate
# Windows PowerShell:
PS venv\Scripts\Activate.ps1

# Prompt changes to show (venv) when active
(venv) $ pip install django pandas requests
Successfully installed django-5.0.1 pandas-2.1.4 requests-2.31.0

# Verify — pip installs ONLY into venv
(venv) $ pip list

# Step 3: Work on your project...

# Step 4: Deactivate when done
(venv) $ deactivate
$ # back to system Python

# Add venv to .gitignore — never commit it!
$ echo "venv/" >> .gitignore
Isolated
No global conflicts
Pinned
Exact versions per project
Reproducible
Works on any machine
Clean
Just delete the folder

requirements.txt — Reproducible Installs

A requirements.txt file lists all your project's dependencies and their exact versions. Anyone who clones your project can recreate the exact same environment with one command: pip install -r requirements.txt. This is the standard way to share and deploy Python projects.

Terminal — requirements.txt workflow
# Generate requirements.txt from current venv
(venv) $ pip freeze > requirements.txt

# View the generated file
(venv) $ cat requirements.txt
certifi==2024.2.2
charset-normalizer==3.3.2
Django==5.0.1
idna==3.6
numpy==1.26.4
pandas==2.1.4
requests==2.31.0

# Install from requirements.txt (on a new machine/teammate's PC)
$ python -m venv venv
$ source venv/bin/activate
(venv) $ pip install -r requirements.txt
Successfully installed Django-5.0.1 pandas-2.1.4 requests-2.31.0 ...
Text — requirements.txt (two formats)
# requirements.txt — exact pinned versions (from pip freeze)
# Best for deployment — guarantees exact same environment
Django==5.0.1
requests==2.31.0
pandas==2.1.4
numpy==1.26.4

# requirements.txt — loose version constraints
# Best for libraries — allows compatible updates
Django>=4.2,<6.0
requests>=2.28
pandas>=2.0
pip freeze vs manual requirements pip freeze captures all installed packages including sub-dependencies — great for deployment. For library development, write requirements manually with flexible version constraints so users aren't locked to exact sub-dependency versions.

Package Management Best Practices

Following these practices from day one saves hours of debugging dependency hell and makes your projects portable, reproducible, and team-friendly.

Practice Why it matters
Always use a virtual environment Prevents conflicts between projects and system Python
Commit requirements.txt, not venv/ Teammates regenerate venv; committing venv bloats the repo
Pin versions for deployment requests==2.31.0 guarantees reproducible production builds
Keep venv out of version control Add venv/ to .gitignore
Upgrade pip first Run pip install --upgrade pip after creating each venv
Check package trust before installing Verify the package on pypi.org — check downloads, stars, last update
Terminal — professional project setup
# Create project directory
$ mkdir my_project && cd my_project

# Create and activate virtual environment
$ python -m venv venv
$ source venv/bin/activate

# Upgrade pip
(venv) $ pip install --upgrade pip

# Install project dependencies
(venv) $ pip install django requests pandas pytest

# Freeze dependencies
(venv) $ pip freeze > requirements.txt

# Create .gitignore
(venv) $ echo "venv/" > .gitignore
(venv) $ echo "__pycache__/" >> .gitignore
(venv) $ echo "*.pyc" >> .gitignore

# Commit requirements.txt, not venv/
(venv) $ git add requirements.txt .gitignore
(venv) $ git commit -m "chore: add requirements.txt"

Practice Python Online — Free Compilers

Don't want to install anything yet? Practice Python and test popular packages directly in your browser with these free environments — most support pip install!

▶
Online Python 3 Compiler
Write, run and debug Python code in your browser. No installation required. Test Python code instantly.
Open Compiler
Replit
Full IDE online
Google Colab
Free GPU notebooks
JupyterLite
Notebooks in browser
Programiz
Beginner-friendly
Chapter Summary
  • pip is Python's built-in package manager. It installs packages from PyPI — the Python Package Index with 500,000+ packages. It ships with Python 3.4+.
  • Core install commands: pip install package, pip install package==1.2.3, pip install --upgrade package, pip install -r requirements.txt.
  • Inspect commands: pip list, pip show package, pip list --outdated. Uninstall: pip uninstall package.
  • Always use the safest form: python -m pip install to ensure you're using the correct Python version's pip.
  • A virtual environment (python -m venv venv) isolates packages per project — prevents version conflicts and keeps your system Python clean.
  • Activate with source venv/bin/activate (macOS/Linux) or venv\Scripts\activate (Windows). Deactivate with deactivate.
  • pip freeze > requirements.txt saves your exact environment. pip install -r requirements.txt recreates it on any machine.
  • Always add venv/ to .gitignore — never commit the virtual environment folder, only requirements.txt.
  • Key packages: requests (HTTP), pandas (data), Django / FastAPI (web), scikit-learn (ML), pytest (testing).
⚡
Free Resource
Download our Python pip & Packages Cheat Sheet
All pip commands, venv workflow, requirements.txt & popular packages — one printable page.
Get Sheet
Advertisement
Quick Check — Test Your Understanding
Question & Answer
✓ Answer

pip (Pip Installs Packages) is Python's official command-line package manager, bundled with Python 3.4+. It downloads, installs, upgrades, and uninstalls Python packages.

PyPI (Python Package Index) is the central online repository at pypi.org that hosts 500,000+ open-source packages. When you run pip install requests, pip fetches the package from PyPI and installs it into your Python environment.

✓ Answer

A virtual environment is an isolated Python environment that has its own Python interpreter, pip, and installed packages — completely separate from your system Python and other projects.

You should always use one because:

  • Different projects can require different package versions without conflict.
  • You don't pollute the system Python with project-specific packages.
  • The environment is reproducible — delete and recreate with requirements.txt.
✓ Answer

Run pip freeze > requirements.txt inside your activated virtual environment. This saves all currently installed packages and their exact versions to the file.

requirements.txt is used to recreate the same environment on another machine (or for a teammate). They simply run:

python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
✓ Answer

Use the == operator to pin an exact version:

pip install requests==2.31.0      # exact version
pip install "requests>=2.28"      # minimum version
pip install "django>=4.0,<5.0"  # version range

Use pip install --upgrade requests to upgrade to the latest version.

✓ Answer

Never commit the venv folder. Virtual environments are large, platform-specific, and can be regenerated from requirements.txt in seconds. Committing them bloats the repository and causes cross-platform issues.

Instead, commit:

  • requirements.txt — so anyone can recreate the exact environment.
  • .gitignore that includes venv/, __pycache__/, and *.pyc.
MCQ 1 of 3
Which command installs a package named "flask" using pip?
MCQ 2 of 3
Which command saves all currently installed packages and versions to a file?
MCQ 3 of 3
Which command creates a new virtual environment named "venv"?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel