
Python
Pip is Python's package manager. It lets you download and install packages from PyPI (Python Package Index) and manage your project dependencies.
Pip is the standard tool for installing Python packages.
# Basic install
pip install requests
# Install specific version
pip install numpy==1.21.0
# Install multiple packages
pip install flask django pytest
# Install with version constraints
pip install "pandas>=1.0,<2.0"
# Upgrade installed package
pip install --upgrade requests
pip install -U requests # ShorthandFinding packages:
# Search for a package
pip search flask # (deprecated, use PyPI website instead)
# Show package information
pip show numpy
# Output:
# Name: numpy
# Version: 1.21.0
# Location: /usr/local/lib/python3.9/site-packagesViewing installed packages:
# List all installed packages
pip list
# List with specific format
pip list --format=json
# Check if a package is installed
pip show pandasUninstalling packages:
# Remove a package
pip uninstall requests
# Remove multiple packages
pip uninstall flask django pytest
# Uninstall without confirmation
pip uninstall -y requestsManage dependencies with `requirements.txt`.
Creating requirements.txt:
# Generate from current environment
pip freeze > requirements.txt
# Manual format
flask==2.0.1
requests>=2.26.0
pytest>=6.2.0
numpy>=1.20.0
pandas>=1.2.0Using requirements.txt:
# Install all dependencies
pip install -r requirements.txt
# Install dev dependencies
pip install -r requirements-dev.txt
# Install with constraints
pip install -r requirements.txt --upgradeDifferent requirements files:
project/
āāā requirements.txt # Core dependencies
āāā requirements-dev.txt # Development tools
āāā requirements-test.txt # Testing tools
āāā requirements-prod.txt # Production settingsrequirements.txt:
flask==2.0.1
sqlalchemy==1.4.0
requests>=2.26.0requirements-dev.txt:
-r requirements.txt # Include core requirements
pytest==6.2.0
black==21.7b0
flake8==3.9.0
pytest-cov>=2.12.0# Install production dependencies
pip install -r requirements.txt
# Install development dependencies
pip install -r requirements-dev.txtUse virtual environments to isolate project dependencies.
# Create virtual environment
python -m venv venv
# Activate (macOS/Linux)
source venv/bin/activate
# Activate (Windows)
venv\Scripts\activate
# Install packages in virtual environment
pip install flask requests
# Deactivate virtual environment
deactivateWhy virtual environments?
# Without venv: packages conflict
# Project A needs: flask==1.0
# Project B needs: flask==2.0
# Can't have both installed globally!
# With venv: each project isolated
project_a/venv/ # has flask==1.0
project_b/venv/ # has flask==2.0
# Both work simultaneously!myproject/
āāā venv/ # Virtual environment (don't commit!)
āāā src/
ā āāā myapp/
ā āāā __init__.py
ā āāā main.py
āāā tests/
ā āāā test_main.py
āāā requirements.txt
āāā README.mdsetup.sh:
#!/bin/bash
# Create and activate virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run application
python src/myapp/main.py# After pip install requests
import requests
response = requests.get("https://api.example.com")
data = response.json()# After pip install numpy
import numpy as np
array = np.array([1, 2, 3, 4, 5])
mean = np.mean(array)# After pip install flask
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/api/data")
def get_data():
return jsonify({"message": "Hello, World!"})
if __name__ == "__main__":
app.run(debug=True)Control which versions of packages are installed.
# Exact version
pip install numpy==1.21.0
# Greater than or equal
pip install "pandas>=1.2.0"
# Less than
pip install "flask<3.0"
# Range
pip install "requests>=2.26.0,<3.0"
# Compatible release
pip install "django~=3.2" # >= 3.2, < 4.0
# Any version
pip install numpyData Science Project:
# requirements.txt
numpy==1.21.0
pandas==1.3.0
matplotlib==3.4.2
scikit-learn==0.24.2
jupyter==1.0.0Web Application:
# requirements.txt
flask==2.0.1
sqlalchemy==1.4.0
flask-sqlalchemy==2.5.1
flask-cors==3.0.10
python-dotenv==0.19.0
gunicorn==20.1.0
# requirements-dev.txt
-r requirements.txt
pytest==6.2.0
black==21.7b0
flake8==3.9.0Testing Setup:
# requirements-test.txt
pytest==6.2.0
pytest-cov==2.12.0
pytest-xdist==2.3.0
hypothesis==6.13.1Issue 1: "No module named 'x'"
# ā Package not installed
python my_script.py # Error: ModuleNotFoundError
# ā
Install the package
pip install requests
# Verify in virtual environment
which python # Should show venv/bin/pythonIssue 2: Version Conflicts
# ā Package requires different version
pip install "numpy>=1.20" "numpy<1.21"
# ā
Use compatible versions
pip install "numpy>=1.20,<2.0"
# Or check compatibility
pip checkIssue 3: Frozen Dependencies
# ā requirements.txt with no versions
flask
requests
# ā
Pin versions for reproducibility
pip freeze > requirements.txt
# Creates exact versions:
# flask==2.0.1
# requests==2.26.0Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep ⢠Build Real ⢠Verify Skills ⢠Launch Forward