pip: Python's Package Manager
pip installs packages from PyPI (Python Package Index) โ over 450,000 packages. It's the primary way to add third-party libraries to your projects.
# Install a package
pip install requests
# Install specific version
pip install requests==2.31.0
# Install from requirements file
pip install -r requirements.txt
# Upgrade a package
pip install --upgrade requests
# Uninstall
pip uninstall requests
# List installed packages
pip list
pip freeze # With exact versions (good for requirements.txt)
# Generate requirements file
pip freeze > requirements.txt
# Show package info
pip show requests
# Search (limited without PyPI web)
pip index versions requests # All available versionspip vs pip3
# On some systems, pip points to Python 2's pip
# Always use pip3 if unsure, or use:
python3 -m pip install requests # Guaranteed to use Python 3's pipVirtual Environments
Virtual environments isolate project dependencies. Each project gets its own Python installation and packages โ no version conflicts between projects.
# Create a virtual environment
python3 -m venv venv # Creates venv/ directory
# Activate
source venv/bin/activate # macOS/Linux
.\venv\Scripts\activate # Windows PowerShell
source venv/Scripts/activate # Windows Git Bash
# Your prompt changes to: (venv) $
# Now you're working in the isolated environment
# Install packages (isolated to this project)
pip install flask requests pandas
# Deactivate
deactivate
# Delete environment (just delete the folder)
rm -rf venv/pyproject.toml: Modern Project Configuration
# pyproject.toml โ modern way to configure Python projects
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.backends.legacy:build"
[project]
name = "my-project"
version = "1.0.0"
description = "A cool Python project"
requires-python = ">=3.9"
dependencies = [
"requests>=2.28.0",
"flask>=2.3.0",
]
[project.optional-dependencies]
dev = [
"pytest>=7.0",
"black>=23.0",
"mypy>=1.0",
]
[tool.black]
line-length = 88
[tool.mypy]
python_version = "3.11"
strict = trueProject Structure Best Practices
# Professional Python project structure
my-project/
โโโ src/
โ โโโ mypackage/
โ โโโ __init__.py
โ โโโ main.py
โ โโโ utils.py
โโโ tests/
โ โโโ __init__.py
โ โโโ test_main.py
โ โโโ test_utils.py
โโโ docs/
โโโ .gitignore
โโโ pyproject.toml
โโโ README.md
โโโ requirements.txt
# .gitignore for Python projects
venv/
__pycache__/
*.pyc
*.pyo
.env
.pytest_cache/
*.egg-info/
dist/
build/Key Takeaways
- One virtual environment per project: activate before working, every time
- requirements.txt = reproducible installs: commit this to git
- pip freeze > requirements.txt: capture exact versions
- pyproject.toml: modern way to configure builds, linters, type checkers
- python3 -m pip: use this form when multiple Python versions are installed
Practice Exercises
- Create a new project directory, set up a virtual environment, and install 3 packages. Generate requirements.txt.
- Create a minimal
pyproject.tomlfor a hypothetical project with 2 dependencies. - Install a package, use it in a script, then create a fresh virtual environment and verify the script fails, then install from requirements.txt and verify it works.