Introduction
Manage Python environments and dependencies for reproducible projects.
Virtual Environments
# Create virtual environment
python -m venv venv
# Activate
source venv/bin/activate # Linux/Mac
venv\Scripts\activate # Windows
# Install packages
pip install package_name
# Save dependencies
pip freeze > requirements.txt
# Install from requirements
pip install -r requirements.txt
Pipenv
# Create environment
pipenv install requests
# Activate shell
pipenv shell
# Lock and sync
pipenv lock
pipenv sync
# Remove environment
pipenv --rm
Poetry
# Create project
poetry new project_name
# Add dependencies
poetry add requests
# Install all
poetry install
# Generate lock file
poetry lock
Practice Problems
- Create and manage virtual environments
- Use requirements.txt effectively
- Choose between pipenv and poetry
- Handle multiple Python versions
- Share reproducible environments