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)

  1. Create an interactive session to a compute node.

interact --account=<account> --mem 16G
  1. 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
  1. Load the desired Python version.

module load Python/3.12.3-GCCcore-13.3.0
  1. Create the virtual environment (VE).

python -m venv ~/path/to/virt-env/<VE name>
  1. Activate the VE. You must specify the activate script within the bin directory of your VE.

source ~/path/to/virt-env/<VE name>/bin/activate
  1. Check the python version in the VE. You should get the same version of python as is in the module.

python --version
  1. Install packages to your VE.

python -m pip install <package_name>
  1. List the packages in the VE.

pip list
  1. Deactivate the VE.

deactivate
  1. Terminate the interactive session on the compute node.

exit