ReviseAlgo Logo

Introduction to Python

Virtual Environments

Managing project dependencies with venv and pip

Interview: Essential for project management

Last Updated: June 12, 2026 7 min read

Virtual environments are isolated Python environments for each project, preventing dependency conflicts between projects that require different versions of the same packages. They are considered essential practice in Python development.

Why Virtual Environments?

Without virtual environments, all projects share the same global Python packages. This causes problems when:

  • Project A needs requests==2.28 but Project B needs requests==2.31
  • A global pip upgrade breaks a project that depended on an older version
  • You can't reproduce someone else's environment from their requirements.txt

Built-in venv Module

Python 3.3+ includes the venv module. It creates a lightweight virtual environment with its own Python binary and site-packages directory.

Alternative Tools

  • virtualenv: Third-party, faster than venv, supports older Python versions. pip install virtualenv.
  • conda: Manages non-Python dependencies too (C libraries, R). Popular in data science.
  • Poetry: Modern dependency management with lock files, version resolution, and publishing. Uses pyproject.toml.
  • Pipenv: Combines pip and virtualenv with Pipfile and Pipfile.lock.
  • uv: Extremely fast Python package installer and resolver written in Rust. Drop-in replacement for pip and venv.

Best Practice

Always name your virtual environment directory .venv (with leading dot) and add it to .gitignore. This keeps it hidden and out of version control. Most IDEs auto-detect .venv as the project interpreter.

Dependency Management

  • pip freeze > requirements.txt — export all installed packages with exact versions
  • pip install -r requirements.txt — install all dependencies from file
  • Split into requirements-dev.txt for testing tools and requirements-prod.txt for production
  • Consider pip-compile from pip-tools for deterministic dependency resolution

Use Cases

Isolating project dependencies between multiple Python projects

Reproducible development environments for team collaboration

CI/CD pipelines requiring clean, predictable package installations

Testing package upgrades without affecting other projects

Common Mistakes

Not using virtual environments and installing packages globally

Committing the .venv directory to version control

Forgetting to activate the virtual environment before installing packages

Not updating requirements.txt after adding new dependencies