A virtual environment is an isolated folder with its own Python packages. Each project gets exactly the library versions it needs, so upgrading pandas for one project never breaks another. It is the first thing professionals set up for every project.
What You Will Learn
- Why virtual environments matter
- Create and activate a venv on Windows, macOS and Linux
- Install packages and freeze requirements
- Recreate an environment from requirements.txt
- Alternatives: conda, uv, Poetry
Prerequisites
Why Use One?
| Without a venv | With a venv |
|---|---|
| All projects share one set of packages | Each project has its own packages |
| Upgrading a library can break older projects | Projects are independent |
| Hard to share exact versions | requirements.txt reproduces the setup |
Create and Activate
# create (once per project)
python -m venv venv
# activate - Windows (Command Prompt)
venv\Scripts\activate
# activate - Windows (PowerShell)
venv\Scripts\Activate.ps1
# activate - macOS / Linux
source venv/bin/activate
# deactivate when done
deactivate
Explanation: When active, your prompt shows (venv) and python/pip point to the environment. On some systems the command is python3. If PowerShell blocks the script, run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once.
Install and Freeze
pip install pandas scikit-learn
pip list
pip freeze > requirements.txt
numpy==2.1.3
pandas==2.2.3
scikit-learn==1.5.2
...
Explanation: pip freeze records the exact versions installed. Commit requirements.txt to Git; never commit the venv folder (add venv/ to .gitignore).
Recreate on Another Computer
python -m venv venv
source venv/bin/activate # or venv\Scripts\activate on Windows
pip install -r requirements.txt
Use It in VS Code and Jupyter
- VS Code: Ctrl+Shift+P → “Python: Select Interpreter” → choose the venv.
- Jupyter:
pip install ipykernelthenpython -m ipykernel install --user --name myprojectand pick that kernel.
Alternatives
| Tool | Best for |
|---|---|
| venv + pip | Built in, simple, standard |
| conda | Data science with non-Python dependencies (CUDA, GDAL) |
| uv | Very fast modern installer and project manager |
| Poetry | Packaging libraries and locking dependencies |
Common Errors
| Problem | Cause and fix |
|---|---|
pip installs globally |
The environment is not activated – check for (venv) in the prompt. |
No module named ... in Jupyter |
The notebook kernel is not the venv; register it with ipykernel. |
| Scripts cannot be loaded (PowerShell) | Change the execution policy as shown above. |
Series
Python series: What is Python · Installation · Syntax · Variables · Operators · Decisions · Loops · Strings · Lists · Dictionary · Functions · Lambda · Modules · Files · Exceptions · OOP · Classes & Objects · Inheritance · Decorators · Generators · Virtual Environments
Interview Questions
Q: Why use virtual environments?
To isolate project dependencies and reproduce environments reliably.
Q: What does pip freeze do?
Lists installed packages with exact versions, usually saved to requirements.txt.
Practice Exercises
- Create a venv for a new project, install requests and save requirements.txt.
- Delete the venv and recreate it from requirements.txt.
Summary
Create a venv per project, activate it, install packages, freeze them to requirements.txt and recreate anywhere with pip install -r.
Next: NumPy · Docker for ML
Machine Learning Tutorials, Courses and Certifications