Python subprocess set shell var. and then run the command - how?

I need to do this:

$ export PYRO_HMAC_KEY=123 $ python -m Pyro4.naming 

So, I found that the second can be done with

 subprocess.Popen(['python','-m','Pyro4.naming']) 

but how to export a shell variable before that?

+7
source share
3 answers

To upgrade an existing environment ...

 import os, subprocess d = dict(os.environ) # Make a copy of the current environment d['PYRO_HMAC_KEY'] = '123' subprocess.Popen(['python', '-m', 'Pyro4.naming'], env=d) 
+15
source

The subprocess functions take an argument env , which can be assigned a mapping of environment variables for use in the process:

 subprocess.Popen(['python','-m','Pyro4.naming'], env={'PYRO_HMAC_KEY': '123'}) 
+8
source
0
source

All Articles