Virtual Environments: How to Build Using pip
and venv
This information is part of a collection of information on building and using virtual environments.
ARC suggests the use of Miniforge as the preferred way to build conda virtual environments (CVEs). We recommend not to install andaconda on your home as suggested on traditional tutorials. ARC’s provided Miniforge will work faster than user-installed anaconda in $HOME.
However, there may be times when you want to go with a different approach to create a virtual environment (VE). One approach is using pip
and venv
. The approach is provided here. Note that the resulting VE is not a conda VE.
Steps for Building a Virtual Environment (VE)
Create an interactive session to a compute node.
interact --account=<account> --mem 16G
Identify the Python versions available on the clusters.
module spider Python/3
-------------------------------------------------------------------------------------------------------------------------------
Python:
-------------------------------------------------------------------------------------------------------------------------------
Description:
Python is a programming language that lets you work more quickly and integrate your systems more effectively.
Versions:
Python/3.11.3-GCCcore-12.3.0
Python/3.11.5-GCCcore-13.2.0
Python/3.12.3-GCCcore-13.3.0
Load the desired Python version.
module load Python/3.12.3-GCCcore-13.3.0
Create the virtual environment (VE).
python -m venv ~/path/to/virt-env/<VE name>
Activate the VE. You must specify the
activate
script within thebin
directory of your VE.
source ~/path/to/virt-env/<VE name>/bin/activate
Check the python version in the VE. You should get the same version of python as is in the module.
python --version
Install packages to your VE.
python -m pip install <package_name>
List the packages in the VE.
pip list
Deactivate the VE.
deactivate
Terminate the interactive session on the compute node.
exit