What is a Virtual Environment?
A Python virtual environment (venv) acts as an isolated container for Python installs and associated packages. It allows for the independent management of packages, decoupling your project from system-wide installations or other project dependencies. The virtual environment provides stability, reproducibility, and portability. You can think of a python virtual environment as the specific laboratory in which you conduct your experiments. Creating one ensures that anyone who comes after you knows how to reproduce your work. The key mantra for analytics engineers: always use a virtual environment. This practice ensures a stable and reproducible project environment, granting control over package versions and update schedules. It also enables independence from the system-wide Python installation.Creating a Virtual Environment
The workflow for creating and using a virtual environment in Python is a relatively straightforward process:- Create the virtual environment
- Activate the virtual environment
- Install packages
- (Optional) Create a requirements file with pip freeze
- (Optional) Ignore the virtual environment in your git repo
![A flowchart infographic of the virtual environment workflow: CREATE with python -m venv, ACTIVATE with source, INSTALL packages with pip, FREEZE with pip, and IGNORE with .gitignore.](https://nimbusintelligence.com/wp-content/uploads/2023/12/python-virtual-environments-1-1024x655.png)
venv
in your project directory. This will create a folder in your project directory that contains all the necessary files to run an isolated install of Python:
![](https://nimbusintelligence.com/wp-content/uploads/2023/12/Project-Directory-1014x1024.png)
python
, the version installed by venv
is called.
# MacOS / Unix
source my_venv/bin/activate
# Windows
my_venv/Scripts/activate
# MacOS / Unix
pip install useful_package
pip
. It’s easy to create one, and just as easy to use pip to reproduce a module library. This requirements file should be added to your version control system.
# Use > to send the results of pip freeze to a text file
pip freeze > requirements.txt
# Use the text file to install dependencies
pip install -r requirements.txt
.gitignore
file.
# In the .gitignore for the entire directory
my_venv/
# In a.gitignore inside the venv directory
*