Basic example:
import os
os.system("python --version")
Better, since stdout and return codes aren't easily processed when using os.system
:
import subprocess
subprocess.run("python --version", shell=True)
Security considerations with using shell=True
:
Unlike some other popen functions, this implementation will never implicitly call a system shell. This means that all characters, including shell metacharacters, can safely be passed to child processes. If the shell is invoked explicitly, via shell=True, it is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities.
Full example:
import subprocess
completed_process = subprocess.run(["python", "--version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if completed_process.returncode != 0:
print(f"Error: {completed_process.stdrr}")
else:
print(f"Success: {completed_process.stdout}")
# Output:
# Success: b'Python 3.8.10\n'
Throw an error if the process returns a non-zero exit code:
import subprocess
completed_process = subprocess.run(["python", "--ver"], stdout=subprocess.PIPE, check=True)
# Output:
Traceback (most recent call last):
File "bigquery_schema_migration.py", line 14, in <module>
completed_process = subprocess.run(["python", "--ver"], stdout=subprocess.PIPE, check=True)
File "/Users/justin.lam/.pyenv/versions/3.8.10/lib/python3.8/subprocess.py", line 516, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['python', '--ver']' returned non-zero exit status 2.