Virtual Environments (venv)
Introduction
When developing Python projects, using virtual environments (venv) is a recommended practice. This document highlights the benefits of using venv, how to create a virtual environment on Linux, and how to install dependencies inside it.
Warning
If you get an error indicating that the number of CPLEX variables is limited, it means you did not create the venv using the special Multivac script, mvac_crear_venv.
This script patches the CPLEX binary from the Python 3.10 educational license to work with Python 3.11, which is the version available in the cluster.
Benefits of Using Virtual Environments
Virtual environments provide several advantages:
Dependency Isolation: - Keeps project dependencies separate from other projects, avoiding library version conflicts.
Reproducibility: - Makes it easier to reproduce the exact same development environment on different machines.
Clean Organization: - Simplifies dependency management and makes project cleanup easier when finishing or distributing work.
Easier Updates: - Lets you update or change dependencies independently for each project without affecting system-wide settings.
Creating a Virtual Environment on Linux
Open a Terminal: - Use your system terminal or an integrated terminal in your development environment.
Go to the Project Folder:
$ cd /route/to/your/project_folder
Create the virtual environment:
$ python3 -m venv venv_folder_name
Activate the virtual environment:
$ source venv_folder_name/bin/activate
5.1. Install dependencies inside the venv (if needed):
(venv)$ pip install dependency_name
5.2. Install dependencies from requirements.txt inside the venv (if needed):
(venv)$ pip install -r requirements.txt
Simplified Virtual Environment Creation on Linux
We have prepared a script that creates a venv and installs dependencies automatically, while also adding the CPLEX license to the venv:
$ mvac_crear_venv
How to activate the venv
To activate the venv, run:
$ source venv/bin/activate
How to check if we are inside the venv
The venv name will appear before your user prompt:
(venv) username@iocex:~/$
How to deactivate the venv
To deactivate it, run the following command (inside the venv):
(venv)$ deactivate
What is a requirements.txt file
The requirements.txt file is a key part of Python development. It is used to manage project dependencies, ensure a consistent development environment, and simplify package version management.
It provides the following advantages:
It provides a detailed list of project dependencies, including package versions. This makes it easy to reproduce the environment on other systems.
Using the contents of requirements.txt, you can install all project dependencies in a single command. This is useful for setting up new development environments or deploying applications to servers.
The versions specified in requirements.txt help maintain consistency across the project. All team members use the same package versions, avoiding compatibility issues.
Keeping requirements.txt up to date makes tracking and managing dependencies easier. Adding, updating, or removing a dependency becomes simple and transparent.
How to generate requirements.txt for your project
Inside your virtual environment, run:
(venv)$ pip freeze > requirements.txt
Now requirements.txt will contain a list of all project dependencies together with their versions.
How to install dependencies from requirements.txt
Inside your virtual environment, run:
(venv)$ pip install -r requirements.txt
This command installs all dependencies listed in requirements.txt.
Example requirements.txt file
To install the dependencies needed for your project, you can use a requirements.txt file. Here is an example including docplex, pandas, and numpy:
cplex==22.1.1.1
docplex==2.25.236
numpy==1.26.2
pandas==2.1.4
This file specifies the exact library versions you want to install. You can adapt it to your project’s specific needs. When used with the script above to create and configure the virtual environment, those specific versions will be installed in your project’s venv.
Example: creating a venv on Windows
This script checks whether Python is installed, and installs it if needed.
Then it checks whether a venv exists, and if not, it creates one and installs requirements.txt.
Finally, it executes a Python script inside the venv.
Save this content in a script named my_script.ps1
# Check whether Python is installed
if (-not (Get-Command python.exe -ErrorAction SilentlyContinue)) {
# If not installed, download and install Python
Write-Host "Python is not installed. Download and install the file that will open. Remember to enable adding PATH to the system."
Start-Process -FilePath "https://www.python.org/ftp/python/3.11.2/python-3.11.2-amd64.exe" -ArgumentList "/quiet", "/norestart" -Wait
# Show a message in the terminal
Write-Host "Press Enter once installation is complete..."
# Wait for Enter
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
}
# Check whether a virtual environment exists
$virtualEnvPath = Join-Path $PSScriptRoot 'venv'
Write-Host $virtualEnvPath
if (-not (Test-Path $virtualEnvPath)) {
# If it does not exist, create a virtual environment
Write-Host "Creating a virtual environment..."
python -m venv $virtualEnvPath
$activateScript = Join-Path $virtualEnvPath 'Scripts\Activate.ps1'
if (Test-Path $activateScript) {
Write-Host "Installing requirements..."
& $activateScript
# Install requirements.txt
pip install -r (Join-Path $PSScriptRoot 'requirements.txt')
# Deactivate the virtual environment
& $activateScript | Out-Null
}
}
# Activate the virtual environment
$activateScript = Join-Path $virtualEnvPath 'Scripts\Activate.ps1'
if (Test-Path $activateScript) {
& $activateScript
# Execute the Python script
if (Test-Path (Join-Path $PSScriptRoot 'main.py')) {
python (Join-Path $PSScriptRoot 'main.py')
} else {
Write-Host "main.py was not found in the current location."
}
} else {
Write-Host "The virtual environment could not be activated. Make sure it was created correctly."
}
To manually activate the venv, run:
$ venv/Source/Activate.bat