I am currently using the subprocess module in python for scripting purposes, and have been unable to execute the command 'module list' despite this working when I run it in shell and despite any other kind of command working fine when using the subprocess module to execute commands.
Two variations I have tried:
p = subprocess.Popen('module list', shell=True)
print(p.communicate()[0])
and
p = Popen(["module", "list"], stdout=subprocess.PIPE)
print(p.communicate()[0])
For commands such as 'echo hello world' and even longer commands with multiple arguments, either of these formats works just fine. Is the terminal I run commands from different from the shell used to execute commands using subprocess? The error I get is as follows:
/bin/bash: line 1: module: command not found
which module
in your shell give you? - Niayesh Iskyprint(subprocess.check_output('module list', shell=True))
orprint(subprocess.run('module list', shell=True, check=True, stdout=PIPE).stdout)
(depending on your Python version)? - Niayesh Iskyenvironment-modules
on my Ubuntu distro using this tutorial, and for me,type module
tells me thatmodule
is a function, which means I can't call it from Python's subprocess directly. - Niayesh Isky