A virtual environment in Python is a self-contained directory that houses a specific Python interpreter along with its own set of installed packages. The purpose of using virtual environments is to isolate Python projects, ensuring that dependencies for one project do not interfere with those of another. This helps manage project-specific dependencies and versions, making it easier to maintain and share code across different projects.
You can create a virtual environment using the venv module
python -m venv my_env
Replace my_env with the desired name for your virtual environment.
The built-in venv module was introduced in Python 3.3 and later versions as a part of the standard library to provide a simple and convenient way to create isolated Python environments.
What happens when you create a virtual environment with python -m venv:
1. Creation of Directory Structure: a new directory is created with the name you provide (e.g., my_env). The directory contains subdirectories like bin (or Scripts on Windows), include, and lib.
2. Isolation of Python Interpreter: the virtual environment includes a copy of the Python interpreter from the global environment. This ensures that the virtual environment has its own Python executable, separate from the system-wide Python installation.
3. Installation of Standard Library: the virtual environment includes a copy of the Python standard library.
4. Installation of pip: the venv module ensures that the virtual environment has its own copy of the pip package manager.
5. Activation Script: a script (activate on both Windows and macOS/Linux) is created inside the bin (macOs/Linux) or Scripts (Windows) directory. Running this script activates the virtual environment.
6. Deactivation Script: a script (deactivate on all platforms) is also provided to deactivate the virtual environment.
7. Isolation of site-packages: the site-packages directory inside the virtual environment’s lib directory is empty initially. When you install packages within the virtual environment using pip, they are added to this directory, ensuring isolation from global packages.
Activating a virtual environment in Python is straightforward, but depends on the platform.
You can do this via the command source my_venv/bin/activate. This uses the ‘activate’ script located in the ‘Scripts’ directory of your virtual environment (my_venv).
Here’s a simple example :
source my_venv/bin/activate
Cmd: execute the activate.bat file in the Scripts directory of your virtual environment.
C:\> <my_venv>\Scripts\activate.bat
Powershell: execute the Activate.ps1 file in the Scripts directory of your virtual environment.
C:\> <my_venv>\Scripts\Activate.ps1
With the virtual environment activated, you can use pip to install packages, and they will be isolated to that specific environment.
pip install package_name
To deactivate the virtual environment and return to the global Python environment, use the following command:
deactivate
Using a requirements.txt file is a good practice when working with Python virtual environments. A requirements.txt file helps you document and manage the dependencies for your Python project. It typically lists the names and versions of Python packages required for a specific project to run correctly. This file is useful for managing and reproducing the exact environment of a project by installing the specified dependencies.
pip install -r requirements.txt --upgrade.Manually: you can create the file yourself using a text editor and list the dependencies one per line. For example
requests==2.26.0
Flask==2.0.1
Automatically: you can use the pip freeze command to generate a requirements.txt file from the currently installed packages in your virtual environment. Run the following command in your terminal with the virtual environment activated:
pip freeze > requirements.txt
To install the dependencies listed in the requirements.txt file, you can use the following command:
pip install -r requirements.txt --upgrade
This command reads the file and installs the specified versions of the packages into your Python environment.
Include the requirements.txt file in your project’s version control system (e.g., Git) so that others can easily recreate the environment.