Skip to content

Loading Modules in a Jupyter Notebook

Interacting with the module system in Jupyter does not always work as expected. Common tactics like opening a shell and loading modules or using Jupyter's "bang" syntax to call a shell command

1
!module load TensorFlow

will not change Python's environment. Instead, you will need to interact with the module system more directly.

Configuration if Using Conda

Due to the way ICER's Conda/3 module works, you will need to make one small configuration change. Assuming you have set the CONDA3PATH environment variable, from a command line, run the following code:

1
2
cd $CONDA3PATH
ln -s conda.sh conda.python

Loading modules in Python

Similarly to how the module system is made available to the bash shell, you can make the module system available in Python directly with an import statement. You will need to add the location of the module to your system path.

1
2
3
import sys
sys.path.append("/usr/lmod/lmod/init")
from env_modules_python import module

After this, the module command can be used as a Python function:

1
2
module("list")
module("load TensorFlow")

Loading modules in Jupyter with jupyter-lmod

You can also integrate the module system directly in Jupyter using the jupyter-lmod extension. You can install this on the command line into a virtual or Conda environment you are loading the Jupyter from with

1
pip install jupyterlmod

Then, when you start the Jupyter notebook, you will have an extra tab on the right where you can search and load modules into your Python execution environment.