Python · Fundamentals & Gotchas

What is a virtual environment?

Easyvenvvirtualenvdependenciespackaging

Interview question

What is a virtual environment in Python?

How to answer

The short answer

A virtual environment is an isolated directory that contains a Python interpreter plus its own site-packages. Projects get separate dependency sets so Package A for Project 1 does not conflict with Package B for Project 2, and you do not pollute the system Python.

Common tools: venv (stdlib), virtualenv, poetry, uv, conda (broader env management).

Python
# Shell examples (not Python code executed by the interpreter)
# python -m venv .venv
# source .venv/bin/activate          # macOS/Linux
# .venv\Scripts\activate             # Windows
# pip install -r requirements.txt
# deactivate

What isolation gives you:

  • Per-project package versions
  • Reproducible installs via requirements.txt / lockfiles
  • Safer experimentation without breaking global tools

Interview tip: Pair "virtual env" with "pin dependencies" and "don't install into system Python for app work."

Lock it in with a real drill

You remember an answer longer once you have used it. Try a hands-on problem on this concept.

Open related drill