Lmod icon indicating copy to clipboard operation
Lmod copied to clipboard

Use lmod in perl script

Open stefandiederich opened this issue 6 years ago • 8 comments

Hi,

I want to use lmod modules within a perl script. Unfortunately I do not get it to work right now. Simply calling system('module load xyz') does not work for me. Do you have any suggestion to solve this problem so I can load and unload modules during runtime of my script?

Thanks in advance Stefan

stefandiederich avatar Mar 05 '18 10:03 stefandiederich

You should call lmod perl load xyz (or $LMOD_CMD perl load xyz), and take the stdout that command produces (should be Perl syntax) to update your environment.

boegel avatar Mar 05 '18 14:03 boegel

Thanks a lot for your quick response. When I execute the command you provided (/BioinfSoftware/lmod/lmod/libexec/lmod perl load GATK) I get the following output:

$ENV{MODULEPATH}="/etc/environment-modules/modules:/usr/share/modules/versions:/usr/Modules/$MODULE_VERSION/modulefiles:/usr/share/modules/modulefiles:/BioinfSoftware/modulefiles/Linux:/BioinfSoftware/modulefiles/Core:/BioinfSoftware/lmod/lmod/modulefiles/Core";
$ENV{PATH}="/BioinfSoftware/GATK/4.0.0:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/BioinfSoftware:/BioinfSoftware/bpipe/bin:/snap/bin";
$ENV{_LMFILES_}="/BioinfSoftware/lmod/lmod/modulefiles/Core/GATK/4.0.0.lua";
$ENV{_ModuleTable001_}="X01vZHVsZVRhYmxlXz17WyJNVHZlcnNpb24iXT0zLFsiY19yZWJ1aWxkVGltZSJdPWZhbHNlLFsiY19zaG9ydFRpbWUiXT1mYWxzZSxkZXB0aFQ9e30sZmFtaWx5PXt9LG1UPXtHQVRLPXtbImZuIl09Ii9CaW9pbmZTb2Z0d2FyZS9sbW9kL2xtb2QvbW9kdWxlZmlsZXMvQ29yZS9HQVRLLzQuMC4wLmx1YSIsWyJmdWxsTmFtZSJdPSJHQVRLLzQuMC4wIixbImxvYWRPcmRlciJdPTEscHJvcFQ9e30sWyJzdGF0dXMiXT0iYWN0aXZlIixbInVzZXJOYW1lIl09IkdBVEsiLH0sfSxtcGF0aEE9eyIvZXRjL2Vudmlyb25tZW50LW1vZHVsZXMvbW9kdWxlcyIsIi91c3Ivc2hhcmUvbW9kdWxlcy92ZXJzaW9ucyIsIi91c3IvTW9kdWxlcy8kTU9EVUxFX1ZFUlNJT04vbW9kdWxlZmlsZXMi";
$ENV{_ModuleTable002_}="LCIvdXNyL3NoYXJlL21vZHVsZXMvbW9kdWxlZmlsZXMiLCIvQmlvaW5mU29mdHdhcmUvbW9kdWxlZmlsZXMvTGludXgiLCIvQmlvaW5mU29mdHdhcmUvbW9kdWxlZmlsZXMvQ29yZSIsIi9CaW9pbmZTb2Z0d2FyZS9sbW9kL2xtb2QvbW9kdWxlZmlsZXMvQ29yZSIsfSxbInN5c3RlbUJhc2VNUEFUSCJdPSIvZXRjL2Vudmlyb25tZW50LW1vZHVsZXMvbW9kdWxlczovdXNyL3NoYXJlL21vZHVsZXMvdmVyc2lvbnM6L3Vzci9Nb2R1bGVzLyRNT0RVTEVfVkVSU0lPTi9tb2R1bGVmaWxlczovdXNyL3NoYXJlL21vZHVsZXMvbW9kdWxlZmlsZXM6L0Jpb2luZlNvZnR3YXJlL21vZHVsZWZpbGVzL0xpbnV4Oi9CaW9pbmZTb2Z0d2FyZS9tb2R1bGVmaWxlcy9Db3JlOi9CaW9pbmZTb2Z0";
$ENV{_ModuleTable003_}="d2FyZS9sbW9kL2xtb2QvbW9kdWxlZmlsZXMvQ29yZSIsfQ==";
$ENV{_ModuleTable_Sz_}="3";

Which part should I take to update my environment from inside my perl skript? I cant find any perl syntax in it, or did I missunderstand you?

stefandiederich avatar Mar 05 '18 15:03 stefandiederich

Sorry, I accidantly closed this issue.

stefandiederich avatar Mar 05 '18 15:03 stefandiederich

@stefandiederich You should take that output (using backticks to run the command in a Perl script, I believe), and just execute that as Perl code, you shouldn't filter it.

They're all $ENV{...}="..."; statements that update your environment.

boegel avatar Mar 05 '18 15:03 boegel

In order to have the module commands make sense in perl you have to "source" the appropriate perl code to make the module command work and that depends on your installation. Since Lmod defines the environment variable $LMOD_PKG to point to the top of the installation you can use that:

#!/usr/bin/env perl
use strict;
require "$ENV{\"LMOD_PKG\"}/init/perl";

module("load git");
module("list");

rtmclay avatar Mar 05 '18 18:03 rtmclay

@boegel I did not get your solution run on my system... @rtmclay Thank you very much! Your solution worked for me! I think we can close this issue now. Thanks to both of you for your quick help

stefandiederich avatar Mar 06 '18 06:03 stefandiederich

Hi again, even though this issue is about 2 years old, I want to reopen it because my question is directly related. 2 years ago I asked how to load a module in a perl script. That is done via the solution of @rtmclay. Now I have to unload a module in a perl script. I thougth it will simply be the same way and work via

module("unload Ananconda");

But this does not unload the Anaconda module. Is there any other way?

Bests Stefan

stefandiederich avatar Mar 23 '20 09:03 stefandiederich

My tests show that

module("unload foobar")

works fine. when done inside a perl script. Please provide a reproducer that shows this issue. Thanks!

rtmclay avatar Mar 23 '20 15:03 rtmclay

Closing this issue as there have been no followup. Feel free to re-open if there is a reproducer.

rtmclay avatar Sep 12 '22 19:09 rtmclay