0

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.

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.

peeebeee
  • 2,541
  • 6
  • 21
  • 26
Bvs Revanth
  • 49
  • 1
  • 7
  • Since `module` is not a valid internal command of a POSIX shell (which is what you are running here), an executable with the name _module_ is searched in your PATH, but there obviously is none. Hence the error message. – user1934428 Apr 13 '20 at 08:11
  • So what should I do to make the command working ? – Bvs Revanth Apr 13 '20 at 10:35
  • Put the direcory where your executable named _module_ is located, into your PATH. – user1934428 Apr 13 '20 at 10:37

1 Answers1

1

module is not an external command but a function to define in the shell/script language used. So to enable the module command within a Python script, you need to initialize it with the following code:

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.

Once initialized, you can call the module function from your script:

module('load', 'modulefile')