Useful Data Tips

Python Virtual Environments: Complete Guide to venv

⏱️ 25 sec read 🐍 Python

Virtual environments isolate project dependencies and prevent version conflicts. Every Python project should use one. Here's how:

1. Why Use Virtual Environments?

2. Creating a Virtual Environment

# Create venv (Python 3.3+)
python -m venv myenv

# Or specify Python version
python3.11 -m venv myenv

# Common names: venv, env, .venv
python -m venv .venv  # Hidden directory

3. Activating the Environment

# Linux/Mac
source myenv/bin/activate

# Windows Command Prompt
myenv\Scripts\activate.bat

# Windows PowerShell
myenv\Scripts\Activate.ps1

# When activated, prompt shows: (myenv) $
# Verify activation:
which python  # Should point to myenv/bin/python

4. Installing Packages

# After activation, install packages
pip install pandas numpy matplotlib

# Install specific versions
pip install pandas==2.0.0
pip install requests>=2.28.0

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

# Upgrade a package
pip install --upgrade pandas

# List installed packages
pip list
pip freeze  # Formatted for requirements.txt

5. Managing Dependencies

# Create requirements.txt
pip freeze > requirements.txt

# Example requirements.txt content:
# pandas==2.0.0
# numpy==1.24.3
# matplotlib==3.7.1

# Install exact versions on another machine
pip install -r requirements.txt

# For development dependencies
pip freeze > requirements-dev.txt

6. Deactivating and Removing

# Deactivate environment
deactivate

# Remove environment (just delete folder)
rm -rf myenv  # Linux/Mac
rmdir /s myenv  # Windows

# Recreate from requirements.txt
python -m venv myenv
source myenv/bin/activate
pip install -r requirements.txt

7. Best Practices

# Project structure
my_project/
├── .venv/              # Virtual environment
├── src/                # Source code
├── tests/              # Tests
├── requirements.txt    # Dependencies
├── .gitignore         # Exclude .venv/
└── README.md

# .gitignore for Python projects
.venv/
venv/
env/
*.pyc
__pycache__/
.pytest_cache/

8. Alternative Tools

# pipenv (combines pip + venv)
pip install pipenv
pipenv install pandas
pipenv shell  # Activate

# poetry (modern dependency management)
pip install poetry
poetry init
poetry add pandas
poetry shell

# conda (for data science)
conda create -n myenv python=3.11
conda activate myenv
conda install pandas numpy

9. Common Issues

# Issue: "python not found"
# Solution: Use python3 or full path
python3 -m venv myenv
/usr/bin/python3 -m venv myenv

# Issue: PowerShell execution policy
# Solution: Run as admin
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

# Issue: Wrong Python version in venv
# Solution: Specify Python explicitly
python3.11 -m venv myenv

# Issue: pip installing globally
# Solution: Ensure venv is activated
# Check with: which pip  # Should show venv path

10. Quick Reference

# Complete workflow
cd my_project
python -m venv .venv
source .venv/bin/activate  # or .venv\Scripts\activate on Windows
pip install pandas numpy
pip freeze > requirements.txt

# On another machine
git clone my_project
cd my_project
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

# Done! Same environment replicated

Best Practices Summary

Pro Tip: Use python -m venv .venv with a dot prefix to hide the folder. Add an alias to your shell: alias activate="source .venv/bin/activate" for quick activation.

← Back to Python Tips