Skip to content

Instantly share code, notes, and snippets.

@poacosta
Created May 1, 2025 09:29
Show Gist options
  • Select an option

  • Save poacosta/ede582ebe6101499e7aa0a3ff2e4edaf to your computer and use it in GitHub Desktop.

Select an option

Save poacosta/ede582ebe6101499e7aa0a3ff2e4edaf to your computer and use it in GitHub Desktop.
Python Virtual Environments Cheatsheet

Python Virtual Environments Cheatsheet

Core Concepts

Virtual environments in Python create isolated spaces where you can install packages without affecting your global Python installation or other projects. These isolated environments contain their own Python binary and package installations, ensuring dependency conflicts don't occur between projects.

Creating Virtual Environments

venv (Built-in)

# Create a virtual environment
python -m venv myenv

# Create with specific Python version
python3.10 -m venv myenv

virtualenv

# Install virtualenv
pip install virtualenv

# Create environment
virtualenv myenv

# Create with specific Python version
virtualenv -p python3.10 myenv

Activating/Deactivating

Windows (Command Prompt)

# Activate
myenv\Scripts\activate.bat

# Deactivate
deactivate

Windows PowerShell

# Check execution policy
Get-ExecutionPolicy

# If restricted, allow scripts to run (run as Administrator)
Set-ExecutionPolicy RemoteSigned

# Activate
.\myenv\Scripts\Activate.ps1

# Deactivate
deactivate

# Create and activate in one command
python -m venv myenv; .\myenv\Scripts\Activate.ps1

# List all environments in directory
Get-ChildItem -Directory | Where-Object {Test-Path "$_\Scripts\activate.ps1"} | Select-Object Name

macOS/Linux

# Activate
source myenv/bin/activate

# Deactivate
deactivate

Package Management

# Install packages
pip install package_name

# Install from requirements file
pip install -r requirements.txt

# Generate requirements file
pip freeze > requirements.txt

# Update package
pip install --upgrade package_name

Environment Management

# List installed packages
pip list

# Show environment info
pip -V  # pip version
python -V  # python version

# Remove virtual environment
# Just delete the environment directory
rm -rf myenv  # Linux/macOS
rmdir /s /q myenv  # Windows Command Prompt
Remove-Item -Recurse -Force myenv  # PowerShell

Environment Variables

# Create .env file in project root
echo "DEBUG=True" > .env

# Access in Python
import os
debug = os.environ.get("DEBUG")

# Use with python-dotenv
pip install python-dotenv

Best Practices

  1. Name Consistently - Use descriptive names for projects
  2. Git Integration - Add venv/ to .gitignore
  3. Requirements - Keep requirements.txt updated
  4. Document - Note Python version and key dependencies
  5. Isolate Projects - One environment per project

Troubleshooting

  • Activation Fails: Check Python version compatibility
  • PowerShell Execution Policy: Run Set-ExecutionPolicy RemoteSigned as admin
  • Permission Issues: Use --user flag with pip or run as admin/sudo
  • Path Problems: Ensure Python is in your PATH variable
  • Dependency Conflicts: Try pip install --upgrade -r requirements.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment