Mypy

Python Virtual Environments are isolated Python installations that maintain separate sets of packages for each project — preventing the "dependency hell" where Project A needs pandas 1.5 and Project B needs pandas 2.0, and installing one breaks the other, by creating independent directories with their own Python binary and site-packages, ensuring that every project has exactly the dependencies it needs without conflicts.

What Are Virtual Environments?

  • Definition: Self-contained directory trees that include a Python installation and a separate set of installed packages — so that `pip install` inside a virtual environment doesn't affect the system Python or other projects.
  • The Problem: Without virtual environments, all Python packages install globally. Project A installs tensorflow==2.10, then Project B installs tensorflow==2.15 (overwriting 2.10), and Project A breaks. This is "dependency hell."
  • The Solution: Each project gets its own isolated environment. Activating an environment switches your PATH so that python and pip point to the environment's copies, not the system's.

Virtual Environment Tools

ToolBuilt-in?Best For
venvYes (Python 3.3+)Standard projects, simplest option
virtualenvNo (pip install)More features than venv, faster creation
condaNo (Anaconda/Miniconda)Scientific computing, non-Python dependencies (CUDA, MKL)
poetryNo (pip install)Dependency resolution + lock files + packaging
pipenvNo (pip install)Pipfile + Pipfile.lock workflow
uvNo (pip install)Blazing fast Rust-based venv + package management

Lifecycle (venv)

# 1. Create virtual environment
python3 -m venv myenv

# 2. Activate
source myenv/bin/activate        # Linux/Mac
myenv\Scripts\activate.bat       # Windows CMD
myenv\Scripts\Activate.ps1       # Windows PowerShell

# 3. Verify (should point to myenv/)
which python
# /path/to/project/myenv/bin/python

# 4. Install packages (isolated to this env)
pip install pandas scikit-learn torch

# 5. Freeze requirements
pip freeze > requirements.txt

# 6. Deactivate (return to system Python)
deactivate

# 7. Reproduce environment elsewhere
python3 -m venv newenv && source newenv/bin/activate
pip install -r requirements.txt

venv vs conda

Featurevenvconda
Python versionUses system PythonCan install any Python version
Non-Python packagesCannot install C librariesCan install CUDA, MKL, FFmpeg
SpeedFast creationSlower (dependency solving)
Disk usageLightweight (~10MB)Heavier (~200MB+)
Best forWeb dev, general PythonData science, ML (scientific stack)

Common Issues and Fixes

IssueCauseFix
Permission denied on activateFile not executable`chmod +x myenv/bin/activate`
PowerShell won't activateExecution policy restriction`Set-ExecutionPolicy Unrestricted -Scope Process`
Wrong Python versionSystem Python usedSpecify: `python3.10 -m venv myenv`
Packages not found after activationForgot to activateCheck `which python` points to venv

Python Virtual Environments are the essential foundation of reproducible Python development — isolating project dependencies to prevent conflicts, enabling reproducible builds through requirements.txt or lock files, and ensuring that every collaborator, CI pipeline, and production server runs the exact same package versions.

Take mypy further

Ask the copilot about this term, or have our engineers assess it against your process.