🐍 Python Beginner

What is virtual environment in Python?

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.