- Create a virtual environment
- Activate the environment
- Install packages into the environment
- Save dependencies to a requirements file
- Recreate the environment from requirements
- Deactivate the environment
- Remove and rebuild the environment
- Notes
Python virtual environments isolate project dependencies so one project does not overwrite or conflict with another. The built-in venv module is the standard way to manage that isolation in many local development workflows.
Create a virtual environment
python -m venv .venv
This creates a .venv directory containing:
- a Python interpreter for the environment
- installed packages for that environment
- activation scripts for different shells
Activate the environment
PowerShell
.venv\Scripts\Activate.ps1
Command Prompt
.venv\Scripts\activate.bat
macOS or Linux
source .venv/bin/activate
After activation, python and pip point to the environment instead of the system interpreter.
Install packages into the environment
python -m pip install snowflake-connector-python pandas
Using python -m pip helps ensure that pip installs into the currently active interpreter.
Save dependencies to a requirements file
python -m pip freeze > requirements.txt
This captures the installed package versions so the environment can be recreated later.
Recreate the environment from requirements
python -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
On Windows, the activation command should match the shell in use.
Deactivate the environment
deactivate
This returns the shell to the default system Python.
Remove and rebuild the environment
If an environment becomes inconsistent, it can be removed and recreated.
rm -rf .venv
python -m venv .venv
python -m pip install -r requirements.txt
On Windows PowerShell:
Remove-Item -Recurse -Force .venv
python -m venv .venv
python -m pip install -r requirements.txt
Notes
- keep
.venvout of version control - store dependency definitions in
requirements.txtor another lock file - activate the environment before running scripts, tests, or package installs
- recreate the environment when package state becomes difficult to debug