Python · The language, not just syntax
Why use a virtual environment?
Short answer
A virtual environment isolates a project's interpreter and installed packages from the system Python and other projects, preventing version conflicts and making dependency sets reproducible.
Each environment has its own site-packages directory and its own python executable. Installing into it does not affect the global interpreter or another project's environment, which is essential when two services need different versions of the same library.
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python -m pip freeze > requirements.lockOperational habits
- Keep .venv out of version control and rebuild it from declared dependencies
- Invoke python -m pip so you install into the active interpreter
- Rebuild environments in CI rather than caching them indefinitely
- Use uv, Poetry or Conda when you need lock files or non-Python dependencies
An environment solves isolation, not reproducibility. Reproducibility needs pinned direct and transitive dependencies, ideally with a lock file and hashes. Committing only a loose requirements.txt with unpinned versions means two installs can resolve differently.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What exactly does activating an environment change?
- 2Why is python -m pip preferred over pip?
- 3How do you make installs reproducible across machines?
- 4When would you use Conda instead of venv?
Say it in the interview
A virtual environment gives a project its own interpreter and site-packages, so dependencies do not collide with the system or other projects. I create it with the project's Python version, install with python -m pip, and keep it out of version control. Isolation is not the same as reproducibility, so I also pin dependencies with a lock file and rebuild the environment in CI.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Python mock interview and get scored feedback on depth, clarity and confidence.