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 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.
pypi.org
package manager
isolated env
import & 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.
# 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.
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.
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.
# 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 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.
Popular Packages Every Python Developer Should Know
The Python ecosystem is vast. Here are the most widely used third-party packages across different domains — these appear in virtually every professional Python project:
# requests — HTTP made easy
import requests
r = requests.get("https://httpbin.org/json")
print(r.json()) # parsed JSON response
# pandas — data analysis
import pandas as pd
df = pd.read_csv("data.csv")
print(df.describe()) # statistical summary
print(df.groupby("city")["sales"].sum())
# pytest — testing (run with: pytest test_myapp.py)
def add(a, b): return a + b
def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0
# Install all at once:
# pip install requests pandas numpy pytest Django FastAPI sqlalchemy
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 |
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!
- 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 installto 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) orvenv\Scripts\activate(Windows). Deactivate withdeactivate. pip freeze > requirements.txtsaves your exact environment.pip install -r requirements.txtrecreates it on any machine.- Always add
venv/to.gitignore— never commit the virtual environment folder, onlyrequirements.txt. - Key packages:
requests(HTTP),pandas(data),Django/FastAPI(web),scikit-learn(ML),pytest(testing).
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.
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.
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
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.
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..gitignorethat includesvenv/,__pycache__/, and*.pyc.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.