bash linux bash脚本中的模块加载命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45224536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:18:39  来源:igfitidea点击:

module load command in linux bash script

pythonlinuxbashenvironment-modules

提问by skwang

I need to execute Python script in HPC cluster. Unfortunately, the default python version is just 2.6.6 and there is no numpy and scipy.

我需要在 HPC 集群中执行 Python 脚本。不幸的是,默认的 python 版本只有 2.6.6,没有 numpy 和 scipy。

I can load these modules in command line

我可以在命令行中加载这些模块

#module load /home/hw1u16/modules/2.7.3

and

module load /home/hw1u16/modules/1.6.2

However, when I write the bash script like this

但是,当我像这样编写 bash 脚本时

module load /home/hw1u16/modules/2.7.3
module load /home/hw1u16/modules/1.6.2
python /home/hw1u16/project/trainAgent.py

It warns me

它警告我

ModuleCmd_Load.c(200):ERROR:105: Unable to locate a modulefile for '/home/hw1u16/modules' ModuleCmd_Load.c(200):ERROR:105: Unable to locate a modulefile for '/home/hw1u16/modules' I don't know what's wrong, could any guys help me?

ModuleCmd_Load.c(200):ERROR:105: Unable to locate a modulefile for '/home/hw1u16/modules' ModuleCmd_Load.c(200):ERROR:105: Unable to locate a modulefile for '/home/hw1u16/modules' 不知道怎么回事,有大佬帮帮我吗?

回答by DavidC.

Okay, I think I know where is the problem. Try type modulefrom the shell to see how moduleit is currently defined in your system. You will receive two options: either it is an alias or a function. This is because the modulecommand is an aliasor shell function.

好的,我想我知道问题出在哪里了。type module从 shell尝试查看module它当前在您的系统中是如何定义的。您将收到两个选项:别名或函数。这是因为module命令是一个aliasor shell function

Say your script is the following running.sh:

说你的脚本如下running.sh

#!/bin/bash  
module load python/2.7.3
python /home/hw1u16/project/trainAgent.py

(It is a good practice to add the shebang)

(添加shebang是一个很好的做法)

To sort out this problem you have two options:

要解决此问题,您有两种选择:

  • Option 1:
  • 选项1:

sourcethe scitpt. In other words, do: source running.sh. This is exactly the same as typing the modulecommand directly into your interactive shell. However, by doing ./running.sh, you are running a new, non-interactive shell. These generally do not have the standard aliases and shell functions set up.

source脚本。换句话说,做:source running.sh。这与module直接在交互式 shell 中键入命令完全相同。但是,通过执行./running.sh,您正在运行一个新的非交互式 shell。这些通常没有设置标准别名和 shell 函数。

  • Option 2:
  • 选项 2:

Find the initialization script that defines the modulecommand and sourceit from the script

从脚本中找到定义module命令和source它的初始化脚本

回答by Christian Seitz

I had a similar problem, and found two solutions:

我遇到了类似的问题,并找到了两个解决方案:

  1. instead of running your script with sh yourscript.shor ./yourscript.sh, you could run it as . yourscript.shThis will source the module correctly and run the script

  2. if you don't want to use . yourscript.sh, you can modify your shebang from #!/bin/shto #!/bin/bashas noted in DavidC's answer and run your script as ./yourscript.shNote that running it as sh yourscript.shwill not work

  1. 而不是使用sh yourscript.sh或运行您的脚本./yourscript.sh,您可以运行它,因为. yourscript.sh这将正确获取模块并运行脚本

  2. 如果您不想使用. yourscript.sh,您可以按照 DavidC 的回答中所述将您的shebang 修改为#!/bin/shto#!/bin/bash并按以下方式运行您的脚本./yourscript.sh请注意,按以下方式运行是sh yourscript.sh行不通的