Setting up a virtual python environment on a HPC

I need that GPU power
Python
R
cloud
Author

jk

Published

September 2, 2025

Introduction

I find myself routinely going back to use DRAC HPCs especially for some GPU-heavy tasks. I’m used to using conda, which is not available for reasons. Instead I need to use virtualenv, which is no problem. But, it gets a bit complicated. For example, I need to use R inside a python virtualenv, so an R install must be available as some environment variable. But I’m not going to manually download and install R because standard programs are already available as modules. The order of setting this up is also important as I found out the hard way.

Load modules

First, load the right modules:

module load StdEnv/2023 python/3.12.4 r/4.5.0

Then, create a new virtualenv:

virtualenv --no-download ~/new_env

Activating:

source ~/new_env/bin/activate

To install packages with pip, it’s probably the best to install from wheels to avoid errors:

pip install --no-index PACKAGE_NAME

Before running compute jobs, all python packages should be installed inside the new_env environment. All of this information is explained well in the wiki.

Submitting slurm jobs the right way

This is a boilerplate for submitting compute jobs to slurm:

module load StdEnv/2023 python/3.12.4 r/4.5.0
export R_LIBS=/home/skim823/.local/R/4.5.0/
source ~/new_env/bin/activate
python script.py

Even before getting to submitting jobs, the first two lines are still relevant for loading and installing packages to the right version of R. I think it’s useful to have a local, version-specific install location path for R packages as explained here. Then, export the right R_LIBS, the one that matches the loaded version. I had trouble with this step because I was exporting the wrong version from my ~/.bashrc file at log in 🙄… Of course, all necessary R packages need to be installed via the R console. Unlike python, there are R “wheels”, so just run install.packages() or BiocManager::install().

This blog was written in appreciation of, but was not endorsed by…

\ (•◡•) /

Back to top