Using modulefiles to set environment variables


Recently I’ve had the need to set env vars and enter a python venv at the same time. Rather than doing this with a shell script, I found modulefiles to be a useful way of achieving all of this.

Modulefiles are little tcl scripts that can be used to configure environment variables in your shell. Modulefiles can be activated, deactivated, and updated on the fly as they keep track of the shell modifications they do. There’s commands for setting and updating environment variables, adding folders to paths, and other useful utils. Best of all, it’s compatible with bash, zsh, fish, and more.

The modules package needs to be installed in your OS. In fedora it’s environment-modules.

Here’s an example to enter a venv, set an additional python path, and add an env var.

#%Module 1.0
set rootdir /home/yiding/projects/tt
setenv TT_METAL_HOME $rootdir/tt-metal
source-sh --ignore function fish $rootdir/.venv/bin/activate.fish
prepend-path PYTHONPATH $rootdir/tt-metal

This can then be loaded on the shell using

module load /home/yiding/modulefiles/foo