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).
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 .
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With