Learn Python virtual environments in VS Code using venv, pip and requirements.txt. Create, activate, select and verify isolated environments for automation projects.
Architecture: Virtual Environment + venv
Virtual Environment + venv
Project Folder
Keep dependencies with one project
mkdir SQF_ReportCreate venv
Create isolated Python environment
python -m venv .venvActivate
Use project Python and pip
.venv\Scripts\activateSelect Interpreter
Point VS Code to .venv
Python: Select InterpreterInstall Packages
Install only project dependencies
python -m pip install pyodbc pandas openpyxlrequirements.txt
Record/rebuild environment
python -m pip freeze > requirements.txt1. Why use a virtual environment?
A virtual environment isolates one project’s Python packages from the global Python installation. This makes training PCs and production report projects more repeatable.
2. Create a project and venv
mkdir SQF_Report
cd SQF_Report
python -m venv .venvThe .venv folder contains a project-specific Python interpreter and site-packages.
3. Activate the environment in VS Code Terminal
.venv\Scripts\activate
python --version
python -m pip --version(.venv).4. Install project packages
python -m pip install pyodbc pandas openpyxl pyinstaller
python -m pip list5. Create and use requirements.txt
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txtThe first command records versions; the second rebuilds them on another authorized PC.
6. readiness check
import sys
import pandas
import pyodbc
import openpyxl print("Python:", sys.executable)
print("pandas:", pandas.__version__)
print("pyodbc:", pyodbc.version)
print("openpyxl:", openpyxl.__version__)
Frequently Asked Questions
What is a Python virtual environment?
A virtual environment is an isolated Python environment for one project, with its own interpreter context and installed packages.
Should I install packages globally or in .venv?
For project work, prefer a virtual environment so dependencies are isolated and reproducible.
What is requirements.txt used for?
It records package requirements so the same project environment can be installed on another machine.
