Python Virtual Environments (venv): Complete
Beginner Guide
What is a Virtual Environment?
A virtual environment (venv) is an isolated Python environment that has its own Python interpreter
and installed packages. It allows each project to have its own dependencies without affecting other
projects on your computer.
Why is venv Important?
1. Prevents package version conflicts between projects.
2. Keeps your global Python installation clean.
3. Makes projects reproducible.
4. Helps teams use the same package versions.
5. Considered a best practice in professional software development and data engineering.
Step 1: Check Python Installation
Open a terminal and run:
python --version
or
python3 --version
Step 2: Navigate to Your Project Folder
Example:
cd my_project
Step 3: Create a Virtual Environment
Run:
python -m venv venv
This creates a folder called 'venv' containing an isolated Python environment.
Step 4: Activate the Virtual Environment
Windows:
venv\Scripts\activate
Linux / macOS:
source venv/bin/activate
After activation, you will usually see (venv) at the beginning of the terminal prompt.
Step 5: Install Packages
Example:
pip install pandas
pip install faker
pip install sqlalchemy
Step 6: Verify Installed Packages
Run:
pip list
Step 7: Save Dependencies
Create a requirements file:
pip freeze > [Link]
This records all installed package versions.
Step 8: Recreate Environment on Another Machine
After activating a fresh venv:
pip install -r [Link]
Step 9: Deactivate the Environment
Run:
deactivate
Typical Data Engineering Project Workflow
1. Create project folder.
2. Create venv.
3. Activate venv.
4. Install dependencies.
5. Build ETL pipeline.
6. Save [Link].
7. Push project to GitHub.
Common Interview Question
Q: Why should you use a virtual environment?
A: To isolate project dependencies, avoid package conflicts, ensure reproducibility, and maintain
clean project environments.