subprocess.run('export FOO=BAR', shell=True)
subprocess.run()
ls
pwd
export
.run()
.call()
.Popen()
os.environ['FOO'] = "BAR"
export
os.environ
It works fine; however, the variable setting only exists in the subprocess. You cannot affect the environment of the local process from a child.
os.environ
is the correct solution, as it changes the environment of the local process, and those changes will be inherited by any process started with subprocess.run
.
You can also use the env
argument to run
:
subprocess.run(["cmdname", "arg1", "arg number 2"], env=dict(FOO='BAR', **os.environ))
This runs the command in a modified environment that includes FOO=BAR
without modifying the current environment.