Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading environment modules within a python script

Tags:

Is there a way for a python script to load and use environment modules? os.system('module load xxx') doesn't work since it executes them in a subshell (at least, I think that's what's happening).

like image 614
marshall.ward Avatar asked Mar 24 '11 23:03

marshall.ward


People also ask

How do you load a module in Python?

To load the default version of Python module, use module load python . To select a particular software version, use module load python/version . For example, use module load python/3.5 to load the latest version of Python 3.5. After the module is loaded, you can run the interpreter by using the command python .

How do I run a Python module from a script?

The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file, like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard and that's it.

How modules are imported in Python with example?

Any Python file can be referenced as a module. A file containing Python code, for example: test.py , is called a module, and its name would be test . There are various methods of writing modules, but the simplest way is to create a file with a . py extension which contains functions and variables.


2 Answers

I know this question's kind of old but it's still relevant enough that I was looking for the answer, so I'm posting what I found that works as well:

At least in the 3.2.9+ sources, you can include the python "init" file to get a python function version of module:

>>> exec(open('/usr/local/Modules/default/init/python.py').read()) >>> module('list') No Modulefiles Currently Loaded. >>> module('load','foo') >>> module('list') Currently Loaded Modulefiles:   1) foo/1.0 

I've been told earlier versions can do the same without the .py extension, but that's second hand, so ymmv.

Alternative "init" file location (from comment by @lib): /usr/share/Modules/init/python.py

To use with Python 3, version 4.0 or later of Environment Modules is required, as that is the first version to have a bug-free Python3-compliant version of the Python init file.

like image 195
jnewman Avatar answered Oct 28 '22 14:10

jnewman


One of our admins was able to solve the problem for me using os.popen() calls to modulecmd:

cmd = os.popen('/path/to/modulecmd python load my-module') exec(cmd) 
like image 23
marshall.ward Avatar answered Oct 28 '22 16:10

marshall.ward