🐍 Python Beginner

What is virtual environment in Python?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for Python development. It reveals whether you understand the building blocks that more complex concepts rely on.

Answer

A virtual environment is an isolated Python environment that has its own Python interpreter, pip, and installed packages — separate from the system Python. This prevents version conflicts between projects (project A needing requests 2.x and project B needing requests 3.x). Create: python -m venv venv. Activate on macOS/Linux: source venv/bin/activate; Windows: venv\Scripts\activate. Deactivate: deactivate. Modern alternatives: virtualenv (faster), conda (for data science, also manages non-Python packages), poetry (dependency management + virtual envs), pipenv. Always add venv/ to .gitignore. Record dependencies: pip freeze > requirements.txt. Install from requirements: pip install -r requirements.txt.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Python codebase.