pip - Package Installer
pip (Pip Installs Packages) is Python’s standard package manager. It allows you to install and manage third-party packages from the Python Package Index (PyPI) and other sources.
What is pip?
pip is a command-line tool that comes with Python (version 3.4+) and allows you to:
- Install packages from PyPI
- Uninstall packages
- List installed packages
- Show package information
- Search for packages
- Manage package versions
Checking pip Installation
First, verify that pip is installed:
pip --version# orpip3 --versionIf pip is not installed, you can install it using:
python -m ensurepip --upgradeBasic pip Commands
Install a Package
pip install package_nameExample:
pip install requestspip install numpy pandas matplotlibInstall Specific Version
pip install package_name==versionpip install requests==2.28.0Install from Version Range
pip install "package_name>=2.0,<3.0"pip install "requests>=2.25.0,<3.0.0"Upgrade a Package
pip install --upgrade package_name# orpip install -U package_nameUninstall a Package
pip uninstall package_nameList Installed Packages
pip listShow Package Information
pip show package_nameSearch for Packages
pip search queryNote
The
pip searchcommand has been disabled on PyPI. Use the web interface at https://pypi.org/ to search for packages.
Installing from Requirements File
Create a requirements.txt file:
requests==2.28.0numpy>=1.20.0pandas==1.5.0matplotlibInstall all packages from the file:
pip install -r requirements.txtCommon pip Options
Install to User Directory
pip install --user package_nameInstall in Development Mode
pip install -e .Ignore Installed Packages
pip install --ignore-installed package_nameNo Cache
pip install --no-cache-dir package_nameVerbose Output
pip install -v package_namePopular Python Packages
Here are some commonly used packages:
Data Science
- NumPy: Numerical computing
- Pandas: Data manipulation and analysis
- Matplotlib: Plotting and visualization
- SciPy: Scientific computing
Web Development
- Flask: Lightweight web framework
- Django: Full-featured web framework
- Requests: HTTP library
Machine Learning
- scikit-learn: Machine learning library
- TensorFlow: Deep learning framework
- PyTorch: Deep learning framework
Utilities
- Pillow: Image processing
- BeautifulSoup4: HTML/XML parsing
- python-dotenv: Environment variables
Installing Packages
Example: Installing NumPy
pip install numpyThen use it in Python:
import numpy as np
# Create an arrayarr = np.array([1, 2, 3, 4, 5])print(arr) # [1 2 3 4 5]
# Perform operationsprint(arr.mean()) # 3.0print(arr.sum()) # 15Example: Installing Requests
pip install requestsimport requests
# Make HTTP requestresponse = requests.get('https://api.github.com')print(response.status_code) # 200Managing Package Versions
Check Installed Version
pip show package_nameInstall Latest Version
pip install --upgrade package_nameInstall Specific Version
pip install package_name==2.1.0Downgrade Package
pip install package_name==1.9.0Requirements Files
Creating requirements.txt
Generate a requirements file from installed packages:
pip freeze > requirements.txtThis creates a file with all installed packages and their versions:
numpy==1.24.0pandas==1.5.3matplotlib==3.6.2requests==2.28.0Installing from requirements.txt
pip install -r requirements.txtVirtual Environments and pip
It’s recommended to use pip within virtual environments:
# Create virtual environmentpython -m venv myenv
# Activate (Windows)myenv\Scripts\activate
# Activate (Linux/Mac)source myenv/bin/activate
# Install packagespip install package_nameTroubleshooting pip
Upgrade pip
python -m pip install --upgrade pipClear pip Cache
pip cache purgeCheck pip Configuration
pip config listInstall from Alternative Source
pip install -i https://pypi.org/simple/ package_namepip Best Practices
- Use virtual environments: Isolate project dependencies
- Pin versions: Use
requirements.txtwith specific versions - Keep pip updated: Regularly upgrade pip itself
- Use requirements files: Track all dependencies
- Test after installation: Verify packages work correctly
Exercises
Exercise 1: Understanding pip Commands
Write the pip command to install the requests package. Then write code to import requests and make a simple GET request to ‘https://httpbin.org/get’, printing the status code.
Understanding pip Commands
# Command: pip install requests
import requests
response = requests.get('https://httpbin.org/get')print(response.status_code)Exercise 2: Requirements File
Create a requirements.txt content with numpy version 1.24.0 and pandas version 1.5.3. Then write the pip command to install from this file.
Requirements File
# requirements.txt content:# numpy==1.24.0# pandas==1.5.3
# Command: pip install -r requirements.txt
print("requirements.txt created")print("Command: pip install -r requirements.txt")