0

I would like to load a module from within a python script. I am using a custom manager called spack which has a spack load <module> functionality that is similar to the regular module load command on linux. I have tried using both os and subprocessing to run these commands, but the result is always that nothing happens, even though I do not get an error message.

Issuing

spack load mpi

makes the module appear in the list resulting from

spack find --loaded

However, calling these commands in python causes nothing to happen at all:

out = os.popen('spack load mpi').read()
out, err = subprocess.Popen(['spack load mpi'], stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
out, err = subprocess.Popen(['spack', 'load', 'mpi'], stderr=subprocess.PIPE, stdout=subprocess.PIPE)

Is there something else I can try? Why does this command only work when I type it directly into my shell session? How can I make this load command apply to the particular shell session I am using at the time to call this python script?

dmmpie
  • 365
  • 2
  • 14
  • Is the `spack` command a real command, or is it an alias? The `module` command is a shell alias the manipulates the environment of the current shell, so running it in a subshell (e.g., via `subprocess.Popen`, or even by `sh -c 'module ...'`) doesn't make any sense. – larsks Feb 18 '22 at 15:53
  • @larsks yes that's what I figured as well. The spack command works similarly to the module command. Do you know if there is a way to access my current shell instance from within python so I can apply these changes by calling a python script instead of the alias directly? – dmmpie Feb 18 '22 at 16:07
  • The only way for your Python code to affect the current shell would be to wrap it in the same `alias` + `eval` trick used by `module` (and presumably by `spack`). – larsks Feb 18 '22 at 16:17
  • @larsks Can you reference the way module handles this issue? I have already tried issuing `eval \`spack load mpi\`` in both `subprocess.Popen` and `os.popen`, but it didn't change anything about the result – dmmpie Feb 18 '22 at 16:20
  • The `eval` has to happen *in your shell* (usually via an alias). If you try it in your Python code it's already too late; your Python code is running in a subprocess and can't modify any state in the parent shell. I suspect that inspect the current `spack` (or `module`) alias would provide some guidance. – larsks Feb 18 '22 at 16:23
  • @larsks actually after taking a look at the spack source code I saw that the program adds it's own spack binary to path so it is accessible via commandline. Is it still possible that there is an alias referenced somewhere that I could modify? – dmmpie Feb 18 '22 at 16:27

0 Answers0