Entry-Level Tooling 4 min read

Python Virtual Environments — venv, pip, and Dependency Management

The Interview Question

What is a virtual environment in Python and why should you use one?

Expert Answer

A virtual environment is an isolated Python installation with its own packages, separate from the system Python and from other projects. When you install packages with pip without a virtual environment, they go into the global Python installation — which means every project on your machine shares the same dependency versions. This breaks when Project A needs Django 4.2 and Project B needs Django 5.0. Virtual environments solve this by creating a self-contained directory with its own Python binary and site-packages folder. The venv module (built into Python 3.3+) creates these environments. When you activate one, your shell's PATH is modified so 'python' and 'pip' point to the virtual environment's copies. Best practice: create a venv for every project, pin your dependencies with pip freeze > requirements.txt or use pyproject.toml, and never install packages globally except for global tools like pipx.

Key Points to Hit in Your Answer

  • Isolates project dependencies from system Python and other projects
  • venv is built-in since Python 3.3 — no external tools needed
  • Activation modifies PATH so 'python' points to the venv
  • requirements.txt with pinned versions ensures reproducible installs
  • pyproject.toml is the modern standard for Python project configuration
  • Never commit the venv directory — add it to .gitignore
  • Alternatives: poetry, conda (for scientific computing), pipenv

Code Example

# Create and use a virtual environment
python -m venv .venv              # create in .venv directory
source .venv/bin/activate         # activate (Linux/Mac)
.venv\Scripts\activate            # activate (Windows)

pip install django requests       # installs into .venv only
pip freeze > requirements.txt     # pin versions

# requirements.txt looks like:
# Django==5.0.3
# requests==2.31.0

# New developer sets up project:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt   # exact same versions

# Deactivate when done
deactivate

What Interviewers Are Really Looking For

This is a fundamentals question but surprisingly many candidates can't explain why virtual environments exist. The key: dependency isolation between projects. Mentioning the shift toward pyproject.toml over requirements.txt shows you're current. If asked about alternatives, know that poetry handles venvs + dependency resolution, and conda is for scientific computing.

Practice This Question with AI Grading

Reading about interview questions is a start — but practicing with real-time AI feedback is how you actually get better. Goliath Prep grades your answers instantly and tells you exactly what you're missing.

Start Practicing Free →