Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 895
scythargon Avatar asked Aug 06 '12 19:08

scythargon


People also ask

How do I run a shell command in Python using subprocess?

Python Subprocess Run Function run() function was added in Python 3.5 and it is recommended to use the run() function to execute the shell commands in the python program. The args argument in the subprocess. run() function takes the shell command and returns an object of CompletedProcess in Python.

How do I run a shell command in Python?

subprocess.Popen() we are using the subprocess. Popen() method to execute the echo shell script using Python. You can give more arguments to the Popen function Object() , like shell=True, which will make the command run in a separate shell.

How do you run multiple commands in Python?

How can I execute multiple commands in one like in python? You can just use semicolons.


1 Answers

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)
like image 125
chepner Avatar answered Oct 11 '22 09:10

chepner