This document access the doc explains how to work with Python virtual environments (venv) to create isolated development environments for Python projects.
- Virtual environments provide isolated Python environments for different projects
- When activated, pip list shows only packages installed in that environment
- You can import only packages that are installed in the active environment
- requirements.txt allows you to recreate the same environment elsewhere
- The environment name (e.g., (.env)) appears in the command prompt when active
- Deactivate with the deactivate command
python -m venv (Built-in)
Source: Bundled with Python (3.3+).
Purpose: Lightweight isolation for Python packages.
Key Traits:
- Only handles Python dependencies (no non-Python packages).
- Uses the system’s Python interpreter by default.
- No built-in dependency resolution (relies on pip).
Use Case: Simple, standardized virtual environments for pure Python projects.
Q: How does python -m venv differ from tools like virtualenv or conda environments?
virtualenv (Third-Party)
Source: External package (pip install virtualenv).
Purpose: Similar to venv but with extended features.
Key Differences from venv:
- Works with older Python versions (pre-3.3).
- Faster creation (optimized code).
- Supports more customization (e.g., --no-pip, --system-site-packages).
- Can create environments with different Python versions (if installed).
Use Case: Legacy systems or when needing finer control over environment setup.
conda (Anaconda/Miniconda)
Source: Part of the Anaconda distribution.
Purpose: Full environment management for Python and non-Python (e.g., R, C libraries).
Key Differences:
- Cross-Language: Manages non-Python dependencies (e.g., NumPy’s C bindings).
- Dependency Resolution: Uses conda’s solver (more robust than pip for complex dependencies).
- Channels: Accesses precompiled binaries via Conda-Forge/Anaconda repos.
- Heavier: Installs a separate Python stack (not tied to the system Python).
Use Case: Data science, ML, or projects mixing Python with other languages/tools.
Top comments (0)