How to work with LMOD modules within a python script?

I’m looking for a way to load/unload LMOD modules from a python script. I thought I found the answer here:

that mentions python init script in LMOD, but it doesn’t seem to fully work. First I add directory with the init script to the PYTHONPATH variable

export PYTHONPATH=${PYTHONPATH}:$LMOD_DIR/…/init

then after opening python I run

from env_modules_python import module

From that commands like module(‘av’) and module(‘list’) work and produce expected output. But loading modules does not work:

module(‘load MATLAB’)
module(‘list’)
No modules loaded

Furthermore, it seems like the problem is with any command that has arguments, e.g.

module(‘av MATLAB’)

produces help page instead of a list of MATLAB modules.
Anyone knows how to fix this issues and load LMOD modules from a python script?

Of course as soon as I post the question I found the answer. Posting it here in case someone else runs into the same issue. My problem was in syntax on the module command, the right way to use it is

module(“load”,“foobar”)

1 Like

I didn’t have the same issue when I went to try it on our system just now. I wonder why ours differ? I suppose it could be different lmod versions. We are using an older version: 7.8.11 from 2018-11-28.

I also found that if your PYTHONPATH variable is unset or empty, like mine is, it may be worth trying the more generic path appending method of the following:

PYTHONPATH="${PYTHONPATH:+${PYTHONPATH}:}$LMOD_DIR/../init"

Some details about the append method here: https://unix.stackexchange.com/a/415028