Pip Package Manager: Install, Manage Python Dependencies
The Python Standard Library is powerful, but its real strength lies in the millions of third-party packages created by the community. To access this vast ecosystem—everything from web frameworks (Django, Flask) to data science libraries (NumPy, Pandas) to HTTP clients (requests)—you need a package manager. Pip is that tool. It downloads and installs packages from PyPI (the Python Package Index) and manages dependencies for your projects. Understanding pip is essential for moving beyond standalone scripts to professional Python development.
Key Takeaways
- Pip ("Pip Installs Packages") is Python's standard package manager; it ships with Python 3.4+
- PyPI (Python Package Index) is the central repository of millions of open-source Python packages
pip install <package>downloads and installs a package plus its dependencies automaticallypip listshows all packages in your current environment (both installed by you and dependencies)pip uninstall <package>removes a package and is safer than manual deletion- Manage project dependencies professionally with
requirements.txtfor reproducible environments
What Are Pip and PyPI?
Pip is a command-line tool—a package installer. When you run pip install requests, pip contacts PyPI (the Python Package Index), downloads the requests library and any packages it depends on, and installs them into your Python environment.
PyPI is a massive, community-hosted repository hosting over 500,000 open-source Python packages. Think of PyPI as an "app store" for Python code. Every popular library—from requests (HTTP) to flask (web framework) to numpy (numerical computing)—is published on PyPI.
When you install a package, pip also automatically installs its dependencies—other packages that the main package requires to function. For example, requests depends on urllib3, certifi, and charset-normalizer. Pip resolves and installs all of these automatically, a process called dependency resolution. This saves you from manually hunting down and installing supporting libraries.
How Do You Install Your First Package with pip install?
The most fundamental pip command is pip install <package-name>. Let's say you want to build a script that fetches data from web APIs. Instead of writing networking code from scratch, you can use the requests library, which simplifies HTTP requests significantly.
To install it:
pip install requests
Pip will output progress messages, then confirm the installation. The requests library and its dependencies are now available in your Python environment. You can immediately import and use it:
# simple_api_call.py
import requests
# Fetch data from the GitHub API (public endpoint, no authentication required)
response = requests.get("https://api.github.com")
print(f"Status Code: {response.status_code}")
if response.status_code == 200:
print("Successfully connected to the GitHub API!")
# The response object contains data from the API
data = response.json()
print(f"API version info: {data.get('current_user_url', 'N/A')}")
Key point: Once installed, packages are available to all Python scripts on your system that use the same Python environment. If you're using virtual environments (recommended), installation is isolated to that environment.
How Do You View and Manage Installed Packages?
After installing packages, you need to see what's in your environment and remove packages you no longer need.
What Does pip list Show?
The pip list command displays all packages currently installed in your Python environment, including their versions:
pip list
Example output:
Package Version
----------------- -------
certifi 2024.2.2
charset-normalizer 3.3.2
idna 3.6
pip 24.0
requests 2.31.0
urllib3 2.1.0
You can see requests (which you installed) along with its dependencies. This list includes both packages you explicitly installed and packages they depend on. Understanding what's installed helps prevent version conflicts and keeps your environment clean.
How Do You Uninstall Packages with pip uninstall?
When a project ends or you install a package by mistake, remove it with pip uninstall:
pip uninstall requests
Pip will display the files it's about to remove and ask for confirmation (y/n). This safety check prevents accidental deletions. After confirmation, the package and its metadata are removed. Note: if other installed packages depend on what you're uninstalling, pip will warn you but allow it—dependency management becomes your responsibility once you uninstall.
Managing Project Dependencies with requirements.txt
For professional projects, you should document all dependencies in a requirements.txt file. This file lists every package your project needs, allowing others (and your future self) to recreate your exact environment:
# requirements.txt
requests==2.31.0
flask==3.0.0
python-dotenv==1.0.0
To install all packages listed in requirements.txt:
pip install -r requirements.txt
To generate requirements.txt from your current environment:
pip freeze > requirements.txt
This practice is essential for professional development, deployment, and team collaboration.
Complete Pip Command Reference Table
| Command | Purpose | Example |
|---|---|---|
pip install <pkg> | Install a package from PyPI | pip install requests |
pip install <pkg>==X.Y | Install a specific version | pip install flask==3.0.0 |
pip list | Show all installed packages | pip list |
pip show <pkg> | Display details about a package | pip show requests |
pip uninstall <pkg> | Remove a package | pip uninstall requests |
pip freeze | Show installed packages in requirements format | pip freeze > requirements.txt |
pip install -r <file> | Install all packages from a file | pip install -r requirements.txt |
pip search <query> | Search PyPI (deprecated; use pypi.org instead) | — |
Real-World Example: Building a Web Scraper
Here's a practical workflow showing pip in action:
# 1. Create a virtual environment (best practice)
python -m venv scraper_env
source scraper_env/bin/activate # On Windows: scraper_env\Scripts\activate
# 2. Install required packages
pip install requests beautifulsoup4
# 3. Check what's installed
pip list
# 4. Create requirements.txt for the project
pip freeze > requirements.txt
# 5. Share project; others can install with:
# pip install -r requirements.txt
Then in your Python script:
# web_scraper.py
import requests
from bs4 import BeautifulSoup
response = requests.get("https://example.com")
soup = BeautifulSoup(response.content, "html.parser")
print(soup.title.string)
Without pip, you would manually locate, download, and configure each library. Pip automates this, making Python development faster and more collaborative.
Frequently Asked Questions
Where does pip store installed packages?
Pip stores packages in your Python's site-packages directory. You can find it by running python -c "import site; print(site.getsitepackages())". Each Python environment (virtual or system) has its own site-packages, keeping packages isolated.
What is the difference between pip and pip3?
On systems with both Python 2 and Python 3, pip may point to Python 2's pip, while pip3 points to Python 3's. Since Python 2 is retired (end-of-life 2020), always use pip3 if you have both versions, or simply pip on modern systems where Python 3 is the default. To check: pip --version.
Can you install multiple versions of the same package?
No, a single environment can have only one version of a package installed at a time. If you need different versions for different projects, use virtual environments. Each project gets its own venv with its own site-packages.
How do you install a package from GitHub instead of PyPI?
Use the git+ URL syntax: pip install git+https://github.com/user/repo.git. This installs directly from the repository. Most developers publish to PyPI, so direct GitHub installation is less common but useful for development versions or private packages.
What does pip freeze do and why is it useful?
pip freeze outputs all installed packages in requirements.txt format (name==version). Redirect it to a file (pip freeze > requirements.txt) to document your environment. When someone clones your project, they run pip install -r requirements.txt to recreate the exact same environment. This ensures reproducibility across machines and collaborators.
Conclusion
Pip is the gateway to Python's ecosystem. Now you understand how to install packages from PyPI, view what's installed, and manage dependencies. As you build larger projects, you'll rely on pip constantly to access specialized libraries for web development, data science, automation, and more.
The next step is learning to manage project dependencies professionally using requirements.txt and virtual environments, which isolates each project's packages. This combination—pip + virtual environments—is the foundation of professional Python development and team collaboration.