5 Virtual Environment in Python
When starting a new Python project, itβs essential to create a virtual environment to manage dependencies and isolate the projectβs Python interpreter. This guide will walk you through setting up a virtual environment using Pythonβs built-in venv
module and managing dependencies with pip
and requirements.txt
. Weβll also cover how to configure the virtual environment in Visual Studio Code (VS Code) for a seamless development experience.
5.1 Why Use venv
?
venv
creates an isolated, sandbox-like Python environment for each of your projects. This is crucial because it allows you to manage dependencies, libraries, and even Python versions on a per-project basis without affecting other Python projects or the global Python environment. This isolation prevents conflicts between project dependencies and ensures consistency across development, testing, and production environments.
For example, if youβre working on two projects that require different versions of the same library, you can create separate virtual environments for each project. This way, you can install the required library versions in their respective environments without worrying about conflicts. In Figure 5.1, we illustrate an example project structure with separate virtual environments for two projects. ProjectA
and ProjectB
each have their own virtual environment (.venvA
and .venvB
, respectively) containing the required packages for the project.
Projects/
βββ ProjectA/
β βββ .venvA/
β β βββ Scripts/
| | | βββ activate
| | | βββ python.exe (this version can be python3.9.exe, for example)
β β βββ Lib/
β β β βββ site-packages/
β β β βββ packageX-v1.0
β β β βββ packageY-v2.0
β β β βββ packageZ-v3.0
β βββ main.py
β
βββ ProjectB/
βββ .venvB/
β βββ Scripts/
| | βββ activate
| | βββ python.exe (this version can be python3.13.exe, for example)
β βββ Lib/
β β βββ site-packages/
β β βββ packageX-v2.0
β β βββ packageY-v1.0
β β βββ packageZ-v4.0
βββ main.py
You can create separate virtual environments for development, testing, and production to ensure consistency across different stages of your project. This practice helps prevent issues caused by differences in dependencies and configurations between environments.
- Development Environment: Used for writing and testing code. It may contain additional tools and libraries for debugging and development.
- Testing Environment: Used for running automated tests to ensure the code works as expected. It should mirror the production environment as closely as possible.
- Production Environment: The final environment where the code runs in a live or production setting. It should be stable and contain only the necessary dependencies.
5.2 Installing venv
On Linux: If youβre using a Debian-based system (like Ubuntu), you can install
venv
viaapt
:On Windows: You donβt need to install
venv
separately as it comes pre-installed with Python 3.3 and later versions. Just ensure you have the correct Python version installed.
5.3 Creating a Virtual Environment
Once venv
is installed, you can create a new virtual environment for your project. The version of Python you use to create the environment will be the default Python interpreter for that environment. For example, to create a virtual environment in a project directory on Windows, run the following command:
If you have multiple Python versions installed, you can specify the version to use when creating the virtual environment. For example, to use Python 3.13, you would run:
The environment will be created in the .venv
directory within your project. This directory will contain the Python interpreter, standard library, and pip
package manager for the virtual environment. If you want to use the systemβs site packages in your virtual environment (i.e., the packages already installed in the global Python environment), you can add the --system-site-packages
flag when creating the virtual environment:
Notice that .venv
is a common convention for naming virtual environments, but you can choose any name you prefer (e.g., venv
, env
, etc.).
In Windows, you can find the path to the Python interpreter by running the command Get-Command python
in PowerShell. This command will show the path to the Python interpreter.
For example, if you have Python 3.13 installed and configured in your PATH as the default Python version, you would see something like this:
PS C:\path\to\your\project> Get-Command python
CommandType Name Version Source
----------- ---- ------- ------
Application python.exe 3.13.150.β¦ C:\Users\UserName\AppData\Local\Programs\Python\Python313\python.exe
If you have multiple Python versions installed, you can check the path to a specific version by running Get-Command python3
(or python2
, etc.):
5.4 Activating the Virtual Environment
To activate the virtual environment:
On Unix systems:
On Windows:
Activation changes your shellβs prompt and configures your environment to use the Python interpreter and libraries from the .venv
directory. You will see the name of the virtual environment in your shell prompt to indicate that itβs active, for example:
5.5 Setting Up in Visual Studio Code (VS Code)
To ensure consistency, set the Python interpreter in VS Code to the one in your virtual environment:
- Open the Command Palette (
Ctrl + Shift + P
). - Type and select
Python: Select Interpreter
. - If the interpreter is not listed, click on
Enter interpreter path...
and navigate to your virtual environmentβs interpreter (e.g.,.venv\Scripts\python.exe
on Windows).
This guarantees that VS Code uses the correct Python interpreter for debugging and running your code.
You might also need to select the kernel for the interactive window when executing Jupyter notebooks:
- Open a Jupyter notebook.
- Click on the kernel name in the top-right corner.
- Select the kernel corresponding to your virtual environment.
5.6 Managing Dependencies
Once your project is set up, you can install dependencies using pip
as usual. The packages will be installed in the virtual environment, keeping them separate from other projects.
When you need to share your project with others, you can export the dependencies to a requirements.txt
file and install them in another environment. This way, you can ensure that your projectβs dependencies are consistent across different environments.
5.6.1 Creating a Requirements File
A requirements.txt
file lists all the Python packages your project depends on. You can create one by running:
This command lists all installed packages in the current environment and saves them to requirements.txt
.
5.6.2 Installing from Requirements
To install all dependencies listed in requirements.txt
, use:
This installs all the packages and their specific versions as listed in your requirements.txt
, ensuring consistency across environments.
Note: To avoid installation errors, make sure the virtual environment is activated and not being used elsewhere (e.g., in a Jupyter Notebook).
5.7 Projects with Multiple Environments
For projects that require multiple virtual environments (e.g., one for development and another for testing), you can create separate environments and manage them accordingly. Each environment can have its own set of dependencies, allowing you to isolate and control the packages used in different stages of your project.
5.8 Avoid Cloud Storage for Virtual Environments
Avoid storing your virtual environment in cloud storage services like Dropbox or Google Drive. These services can cause issues with file locking and synchronization, leading to corruption or conflicts in the virtual environment.
For example, if you activate the virtual environment on one machine and then switch to another machine without syncing the environment, you may encounter issues due to the differences in the environment paths and configurations.
5.9 Exclude Virtual Environment from Version Control
When working with virtual environments, itβs essential to exclude the environment directory (e.g., .venv
) from version control systems like Git. Instead, include a requirements.txt
file to manage dependencies. This practice ensures that collaborators can recreate the environment easily without sharing unnecessary files.
For example, you can add the virtual environment directory to the .gitignore
file to prevent it from being tracked by Git: