DEV Community

Himanshi
Himanshi

Posted on

Python Virtual Environment Setup Guide

This document access the doc explains how to work with Python virtual environments (venv) to create isolated development environments for Python projects.

  1. Virtual environments provide isolated Python environments for different projects
  2. When activated, pip list shows only packages installed in that environment
  3. You can import only packages that are installed in the active environment
  4. requirements.txt allows you to recreate the same environment elsewhere
  5. The environment name (e.g., (.env)) appears in the command prompt when active
  6. Deactivate with the deactivate command

python -m venv (Built-in)

Source: Bundled with Python (3.3+).
Purpose: Lightweight isolation for Python packages.

Key Traits:

  1. Only handles Python dependencies (no non-Python packages).
  2. Uses the system’s Python interpreter by default.
  3. 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:

  1. Works with older Python versions (pre-3.3).
  2. Faster creation (optimized code).
  3. Supports more customization (e.g., --no-pip, --system-site-packages).
  4. 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:

  1. Cross-Language: Manages non-Python dependencies (e.g., NumPy’s C bindings).
  2. Dependency Resolution: Uses conda’s solver (more robust than pip for complex dependencies).
  3. Channels: Accesses precompiled binaries via Conda-Forge/Anaconda repos.
  4. 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)