Home Knowledge Base Python Virtual Environments

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?

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 executablechmod +x myenv/bin/activate
PowerShell won't activateExecution policy restrictionSet-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.

mypytype checkstatic

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.