Difference between revisions of "Environment modules"
import>Jss43 m |
import>Jss43 m (→python: correct filenames) |
||
Line 48: | Line 48: | ||
If you use ipython (if you don't, you should), then you can implement the module commands as a "magic" command (type ''magic'' at the ipython prompt). The script needed is [[Media:Modules_magic.txt | here]]---save it to ~/.ipython/modules_magic.py. You can add it to the standard profile (ipythonrc): |
If you use ipython (if you don't, you should), then you can implement the module commands as a "magic" command (type ''magic'' at the ipython prompt). The script needed is [[Media:Modules_magic.txt | here]]---save it to ~/.ipython/modules_magic.py. You can add it to the standard profile (ipythonrc): |
||
− | <pre>execfile ~/.ipython/ |
+ | <pre>execfile ~/.ipython/modules_magic.py</pre> |
If you are using a later version of ipython, then there will be a ipy_user_conf.py file in your .ipython directory. To implement the module magic command in this case, add |
If you are using a later version of ipython, then there will be a ipy_user_conf.py file in your .ipython directory. To implement the module magic command in this case, add |
||
− | <pre>import_all(" |
+ | <pre>import_all("modules_magic")</pre> |
in the main() subroutine. |
in the main() subroutine. |
||
Revision as of 16:30, 5 July 2008
The sector notes and the man pages are incredibly useful.
Initialising modules
A shell script inherits the environment its parent, so whatever modules you have loaded are available to any scripts you run. However, the module system is not initialised for non-interactive shells, so a script does not (by default) have access to the module system (i.e. can't change the environment). If you want to be able to change modules in a script, you must initialise the module system: how to do so depends on the language.
Scripts run via cron are started using a non-interactive environment, but have nothing to inherit from. This means if you want to use anything contained in a module in such scripts, you must first initialise the module system and then load the required modules. Initialising the module system does *not* purge the existing modules (see below).
Any modules loaded by a child process do not persist beyond the end of the child process: if a script loads a module, it won't remain in scope once the script ends.
bash
. /usr/share/modules/init/bash initialises modules in bash.
jss43@keiko:~> module list Currently Loaded Modulefiles: 1) pgi/64/7.0/7 2) g95/64/64i/0.91 3) mathematica/6.0.3 jss43@keiko:~> more module_examples . /usr/share/modules/init/bash # This initialises the module system in bash. module load ifort module list jss43@keiko:~> bash module_examples Currently Loaded Modulefiles: # module_examples loads the ifort module and inherits modules already loaded. 1) pgi/64/7.0/7 3) mathematica/6.0.3 2) g95/64/64i/0.91 4) ifort/32/9.1/040 jss43@keiko:~> module list Currently Loaded Modulefiles: # The ifort module does not remain loaded beyond the end of module_examples. 1) pgi/64/7.0/7 2) g95/64/64i/0.91 3) mathematica/6.0.3
python
Similarly to bash, in /usr/share/modules/init there is a python script to initialise the module system. It doesn't work due to a bug in modules' auto-configuration causes an incorrect path to be set. A correct version can be downloaded here---save it as python_modules.py. You can initialise modules using either execfile (specifying the full path) or import (where it must be in the current directory or in your PYTHONPATH, and the .py is left out of the module name). module('command') then works, where command is one of the usual module commands we use in the terminal.
jss43@keiko:~> python >>> execfile('python_modules.py') >>> modules('list') Currently Loaded Modulefiles: 1) pgi/64/7.0/7 2) g95/64/64i/0.91 3) mathematica/6.0.3 jss43@keiko:~> python >>> from python_modules import * >>> modules('list') Currently Loaded Modulefiles: 1) pgi/64/7.0/7 2) g95/64/64i/0.91 3) mathematica/6.0.3
If you use ipython (if you don't, you should), then you can implement the module commands as a "magic" command (type magic at the ipython prompt). The script needed is here---save it to ~/.ipython/modules_magic.py. You can add it to the standard profile (ipythonrc):
execfile ~/.ipython/modules_magic.py
If you are using a later version of ipython, then there will be a ipy_user_conf.py file in your .ipython directory. To implement the module magic command in this case, add
import_all("modules_magic")
in the main() subroutine.
You now have the module command defined in ipython which can be used exactly as in bash:
jss43@keiko:~> ipython Python 2.5 (r25:51908, Jan 10 2008, 18:01:52) Type "copyright", "credits" or "license" for more information. IPython 0.8.3.svn.r3001 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: module list Currently Loaded Modulefiles: 1) pgi/64/7.0/7 2) g95/64/64i/0.91 3) mathematica/6.0.3