scythargon
scythargon

Reputation: 3491

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

I need to do this:

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

So, i found that the second one is possible to do with

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

but how export shell variable before that?

Upvotes: 10

Views: 18612

Answers (3)

chepner
chepner

Reputation: 532093

To update the 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)

Upvotes: 29

Brenden Brown
Brenden Brown

Reputation: 3235

Try os.putenv(): http://docs.python.org/library/os.html#os.putenv

Upvotes: 0

Andrew Clark
Andrew Clark

Reputation: 208635

The subprocess functions accept an env argument that can be given a mapping of environment variables to use in the process:

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

Upvotes: 9

Related Questions