Python PIP
Learn how to use PIP (Python Package Installer) to install and manage Python packages.
What is PIP?
PIP is a package manager for Python packages, or modules if you like.
Note: If you have Python version 3.4 or later, PIP is included by default.
What is a Package?
A package contains all the files you need for a module.
Modules are Python code libraries you can include in your project.
Check if PIP is Installed
Navigate your command line to the location of Python's script directory, and type the following:
Check PIP version:
pip --versionIf PIP is installed, you will see something like this:
pip 23.3.1 from /usr/local/lib/python3.11/site-packages/pip (python 3.11)Install PIP
If you do not have PIP installed, you can download and install it from this page: https://pypi.org/project/pip/
Download a Package
Downloading a package is very easy.
Open the command line interface and tell PIP to download the package you want.
Example - Download a package named "camelcase":
pip install camelcaseNow you have downloaded and installed your first package!
Using a Package
Once the package is installed, it is ready to use.
Import the "camelcase" package into your project.
Example - Import and use "camelcase":
import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))Find Packages
Find more packages at https://pypi.org/.
You can also search for packages using pip:
Search for packages (deprecated in newer versions):
# Note: pip search was deprecated
# Use the PyPI website instead: https://pypi.org/Remove a Package
Use the uninstall command to remove a package:
Example - Uninstall the package named "camelcase":
pip uninstall camelcaseThe PIP Package Manager will ask you to confirm that you want to remove the camelcase package:
Uninstalling camelcase-0.2:
Would remove:
/usr/local/lib/python3.11/site-packages/camelcase/*
Proceed (y/n)?Press y and the package will be removed.
List Packages
Use the list command to list all the packages installed on your system:
Example - List installed packages:
pip listResult:
Package Version
---------- -------
camelcase 0.2
mysql-connector-python 8.0.29
pip 23.3.1
setuptools 65.5.0Show Package Information
Use the show command to display information about a specific package:
Example - Show package information:
pip show camelcaseResult:
Name: camelcase
Version: 0.2
Summary: Convert string into camelCase
Home-page: https://github.com/heynemann/camelcase
Author: Bernardo Heynemann
Author-email: heynemann@gmail.com
License: MIT
Location: /usr/local/lib/python3.11/site-packages
Requires:
Required-by:Advanced PIP Commands
Install Specific Version
# Install specific version
pip install django==4.2.0
# Install minimum version
pip install django>=4.0.0
# Install version range
pip install "django>=4.0.0,<5.0.0"Upgrade Packages
# Upgrade a package to latest version
pip install --upgrade django
# Upgrade pip itself
pip install --upgrade pip
# Show outdated packages
pip list --outdatedInstall from Requirements File
# Install packages from requirements.txt
pip install -r requirements.txt
# Generate requirements file
pip freeze > requirements.txtWorking with Requirements Files
Requirements files are used to specify project dependencies:
Example - requirements.txt file:
# Web framework
django==4.2.7
djangorestframework==3.14.0
# Database
psycopg2-binary==2.9.7
# Development tools
pytest==7.4.3
black==23.11.0
flake8==6.1.0
# Data processing
pandas>=1.5.0,<2.0.0
numpy>=1.24.0
# Optional dependencies
requests[security]>=2.31.0Install from requirements file:
pip install -r requirements.txtGenerate requirements file from current environment:
pip freeze > requirements.txtVirtual Environments
Virtual environments help manage dependencies for different projects:
Create and use virtual environment:
# Create virtual environment
python -m venv myproject_env
# Activate virtual environment (Windows)
myproject_env\Scripts\activate
# Activate virtual environment (macOS/Linux)
source myproject_env/bin/activate
# Install packages in virtual environment
pip install django requests
# Deactivate virtual environment
deactivate
# Remove virtual environment
rm -rf myproject_env # Linux/macOS
rmdir /s myproject_env # WindowsPIP Configuration
Configure PIP behavior with configuration files:
Example - pip.conf (Linux/macOS) or pip.ini (Windows):
[global]
timeout = 60
index-url = https://pypi.org/simple/
trusted-host = pypi.org
pypi.python.org
files.pythonhosted.org
[install]
upgrade = true
user = falseConfiguration file locations:
# Global configuration
# Linux/macOS: /etc/pip.conf
# Windows: C:\ProgramData\pip\pip.ini
# User configuration
# Linux/macOS: ~/.pip/pip.conf or ~/.config/pip/pip.conf
# Windows: %APPDATA%\pip\pip.ini
# Virtual environment configuration
# $VIRTUAL_ENV/pip.confCommon PIP Commands Reference
Popular Python Packages
requests
HTTP library for making API calls
pip install requestsnumpy
Numerical computing library
pip install numpypandas
Data manipulation and analysis
pip install pandasmatplotlib
Plotting and visualization
pip install matplotlibdjango
Web framework
pip install djangoflask
Lightweight web framework
pip install flaskbeautifulsoup4
HTML/XML parsing
pip install beautifulsoup4pillow
Image processing
pip install pillowTroubleshooting PIP
Common Issues and Solutions
Permission errors (use --user flag):
pip install --user package_nameSSL certificate errors:
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org package_nameClear pip cache:
pip cache purgeForce reinstall:
pip install --force-reinstall package_nameInstall from source:
pip install --no-binary :all: package_nameVerbose output for debugging:
pip install -v package_nameBest Practices
- Always use virtual environments for projects
- Pin package versions in requirements.txt for reproducible builds
- Regularly update packages to get security fixes
- Use
pip checkto verify dependency compatibility - Keep requirements.txt files up to date
- Use
--userflag for personal installations - Consider using tools like pipenv or poetry for advanced dependency management
Everyday pip Commands
| Command | Does |
|---|---|
pip install requests | Install a package |
pip install requests==2.31.0 | Install a specific version |
pip install --upgrade requests | Upgrade to the latest |
pip uninstall requests | Remove a package |
pip list | List installed packages |
pip show requests | Details about one package |
If pip is not found, run it through Python: python -m pip install requests (use py -m pip on Windows). This guarantees you install into the Python you are actually running.
requirements.txt: Reproducible Installs
Pin your project's dependencies in a file so anyone can recreate the exact environment.
# save current environment
pip freeze > requirements.txt
# requirements.txt might contain:
# requests==2.31.0
# pandas==2.2.0
# install everything from it
pip install -r requirements.txtTry It Yourself
Exercise 1: Install the requests library and confirm it is installed.
Show solution
pip install requests
pip show requests # prints name, version, locationExercise 2: Freeze your current packages into a requirements file.
Show solution
pip freeze > requirements.txtKey Takeaways
pipinstalls packages from the Python Package Index (PyPI).- Use
python -m pipto target the right interpreter. - Pin dependencies in
requirements.txt; install with-r. - Always install into a virtual environment (next chapter for that topic).
📘 Real-World Deep Dive
<code>pip</code> is the default Python package manager and the first thing every Python project needs to know. Knowing the modern <code>pyproject.toml</code>-driven workflow, the resolver, and the editable-install pattern is the difference between a working setup and a slow week of debugging.
Real-Life Scenario
A reproducible install flow: read the lockfile, install only what is in it, then sanity-check that the entrypoint script works.
Real-Life Example
import subprocess, sys
from pathlib import Path
LOCK = Path("requirements.txt")
def pip(*args: str) -> None:
cmd = [sys.executable, "-m", "pip", "--disable-pip-version-check", *args]
print("$ " + " ".join(cmd))
subprocess.run(cmd, check=True)
if not LOCK.exists():
print(f"missing {LOCK}; aborting")
sys.exit(1)
pip("install", "--upgrade", "pip", "setuptools", "wheel")
pip("install", "-r", str(LOCK))
# Verify
import importlib.metadata as md
for dist in ["fastapi", "uvicorn", "pydantic"]:
try:
print(f"{dist:>10}: {md.version(dist)}")
except md.PackageNotFoundError:
print(f"{dist:>10}: NOT INSTALLED")Expected Output
$ python -m pip --disable-pip-version-check install -r requirements.txt
Successfully installed ...
fastapi: 0.115.0
uvicorn: 0.30.5
pydantic: 2.9.2Common mistakes
- Always invoke
python -m piprather than barepip— guarantees the right interpreter. - Installing into the system Python breaks system tooling; always use a virtualenv.
--no-cache-diris fine in CI but slows local dev iterations.
🚀 Performance & Best Practices
- Use
--require-hashesfor supply-chain safety in CI. - Use
--no-depswhen installing custom-built wheels. uv pip installis 10–100× faster thanpip installfor big projects.
🧪 Try It Yourself
- Compare
pip-compilevs.uv pip compileoutputs on the same source. - Write a test that fails if any
requirements.txtpackage is unpinned. - Build a wheel for an in-repo package with
python -m buildand install with--no-deps.