[英]I'm getting an error “module not found ” when I'm trying to execute shell commands through a python script
When the below statement is executed执行以下语句时
out1 = subprocess.run("module load python",shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
This error is generated.生成此错误。
/bin/sh: module:command not found. /bin/sh:模块:命令未找到。
I want to execute shell commands using a python script and this above statement is not working, but when I execute the same statement on the shell then it is working fine and no error is generated.我想使用 python 脚本执行 shell 命令,上面的语句不起作用,但是当我在 shell 上执行相同的语句时,它工作正常并且没有生成错误。
module
is not an external command but a function to define in the shell/script language used. module
不是外部命令,而是 function 以使用的 shell/脚本语言定义。 So to enable the module
command within a Python script, you need to initialize it with the following code:因此,要在 Python 脚本中启用module
命令,您需要使用以下代码对其进行初始化:
import os
exec(open('/usr/share/Modules/init/python.py').read())
Replace /usr/share/Modules/init
, by the location where the python.py
script (from the environment-modules software) is installed on your machine.将/usr/share/Modules/init
替换为您机器上安装python.py
脚本(来自 environment-modules 软件)的位置。
Once initialized, you can call the module
function from your script:初始化后,您可以从脚本中调用module
function:
module('load', 'modulefile')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.