0

I have an iPython code that uses exclamation mark to run a command:

!aws kms encrypt ...

When I run this command using the subprocess module, it has access to a different set of environment variables. Since my code is in a module, I would like to make it Python compatible, so I would like to replace the exclamation mark (!) with Python code. What is the equivalent Python code for !?

tothsa
  • 53
  • 6
  • 2
    I don't think IPython's `!` does anything different from `subprocess` regarding environment variables. If you observed a difference in environment variables in the subprocess, that was likely due to a difference in your Python process's environment variables. – user2357112 Nov 13 '20 at 10:33

1 Answers1

0

It seems that there is actually no difference between ! and running the command using subprocess, so these 2 solutions seems to be equivalent:

!pwd

and

import subprocess
out = subprocess.run("pwd", shell=True, stdout=subprocess.PIPE)
print(out.stdout)
tothsa
  • 53
  • 6
  • 1
    You don't want or need the `shell=True` there; better `result = subprocess.run(['pwd'], capture_output=True, text=True, check=True); print(result.stdout)`. See also https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess – tripleee Nov 13 '20 at 10:44