Python pip Tutorial - Install Packages, Virtual Environment
What is PIP?
PIP (Pip Installs Packages) is Python's package manager. It lets you install, update, and remove third-party libraries from PyPI (Python Package Index) - a repository of over 500,000 packages.
Basic PIP Commands
# Check pip version
pip --version
pip3 --version
# Install a package
pip install requests
pip install numpy
pip install "flask==3.0.0" # specific version
pip install "django>=4.0,<5.0" # version range
# Install multiple packages
pip install requests flask numpy
# Upgrade a package
pip install --upgrade requests
# Uninstall a package
pip uninstall requests
# List installed packages
pip list
# Show package info
pip show requests
# Search for packages (deprecated in newer pip, use pypi.org)
pip search flask
requirements.txt
A requirements.txt file lists all project dependencies. This is the standard way to share and reproduce project environments.
# Generate requirements.txt from current environment
pip freeze > requirements.txt
# Install all packages from requirements.txt
pip install -r requirements.txt
# Install with exact versions (reproducible)
pip install -r requirements.txt --no-deps
# requirements.txt example
requests==2.31.0
flask==3.0.0
numpy>=1.24.0
pandas>=2.0.0,<3.0.0
python-dotenv
pytest>=7.0
Virtual Environments
A virtual environment is an isolated Python environment for each project. This prevents package version conflicts between projects.
# Create a virtual environment
python -m venv venv
# Activate (Windows)
venv\Scripts\activate
# Activate (macOS/Linux)
source venv/bin/activate
# Your prompt changes to: (venv) $
# Now install packages - they go into venv only
pip install requests flask
# Deactivate when done
deactivate
# Delete the environment (just delete the folder)
rm -rf venv
Popular Python Packages
| Package | Category | Description |
|---|---|---|
requests | HTTP | Simple HTTP requests |
flask | Web | Lightweight web framework |
django | Web | Full-featured web framework |
fastapi | Web/API | Modern async API framework |
numpy | Data Science | Numerical computing |
pandas | Data Science | Data analysis and manipulation |
matplotlib | Data Science | Data visualization |
scikit-learn | ML | Machine learning algorithms |
tensorflow | ML/AI | Deep learning framework |
pytest | Testing | Testing framework |
sqlalchemy | Database | SQL toolkit and ORM |
pillow | Images | Image processing |
beautifulsoup4 | Web Scraping | HTML/XML parsing |
python-dotenv | Config | Load .env files |
import requests
# GET request
response = requests.get("https://api.github.com/users/python")
print(response.status_code) # 200
data = response.json()
print(data["name"]) # Python
# POST request
payload = {"username": "alice", "password": "secret"}
response = requests.post("https://httpbin.org/post", json=payload)
print(response.json())
# With headers and timeout
headers = {"Authorization": "Bearer TOKEN"}
response = requests.get(
"https://api.example.com/data",
headers=headers,
timeout=10
)
# Error handling
try:
response = requests.get("https://example.com", timeout=5)
response.raise_for_status() # raises for 4xx/5xx
print(response.text)
except requests.exceptions.Timeout:
print("Request timed out")
except requests.exceptions.HTTPError as e:
print(f"HTTP error: {e}")
Level Up Your Python Skills
Master Python with these hand-picked resources
10,000+ learners
Free forever
Updated 2026
Related Python Topics