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 --version If 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 camelcase Now 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 camelcase The 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 list Result:
Package Version
---------- -------
camelcase 0.2
mysql-connector-python 8.0.29
pip 23.3.1
setuptools 65.5.0 Show Package Information
Use the show command to display information about a specific package:
Example - Show package information:
pip show camelcase Result:
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 --outdated Install from Requirements File
# Install packages from requirements.txt
pip install -r requirements.txt
# Generate requirements file
pip freeze > requirements.txt Working 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.0 Install from requirements file:
pip install -r requirements.txt Generate requirements file from current environment:
pip freeze > requirements.txt Virtual 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 # Windows PIP 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 = false Configuration 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.conf Common PIP Commands Reference
Popular Python Packages
requests
HTTP library for making API calls
pip install requests numpy
Numerical computing library
pip install numpy pandas
Data manipulation and analysis
pip install pandas matplotlib
Plotting and visualization
pip install matplotlib django
Web framework
pip install django flask
Lightweight web framework
pip install flask beautifulsoup4
HTML/XML parsing
pip install beautifulsoup4 pillow
Image processing
pip install pillow Troubleshooting PIP
Common Issues and Solutions
Permission errors (use --user flag):
pip install --user package_name SSL certificate errors:
pip install --trusted-host pypi.org --trusted-host pypi.python.org --trusted-host files.pythonhosted.org package_name Clear pip cache:
pip cache purge Force reinstall:
pip install --force-reinstall package_name Install from source:
pip install --no-binary :all: package_name Verbose output for debugging:
pip install -v package_name Best 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