subprocess.run('export FOO=BAR', shell=True)
os.environ['FOO'] = "BAR"
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
You can also use the
env argument to
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.