Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control process environment variable in Python?

Tags:

python

I'm trying to change the environment of my Python execution process. It seems that the correct way to do that should be to interact with os.environ. However, I the following assertion fails:

import os, subprocess
os.environ['ATESTVARIABLE'] = 'value'
value = subprocess.check_output(['echo', '$ATESTVARIABLE'], shell=True)
assert 'value' in value

Is there something else that I should be doing to change the current environment? What flaw in my understanding of Python is revealed by the above code :)?

(Note that within the current Python interpreter, os.environ['ATESTVARIABLE'] contains the expected value. I am setting up to run some code which requires a specific environment variable, and which may launch external processes. Obviously, if I wanted to control the environment of a specific subprocess, I'd use the env keyword.)

like image 535
dbn Avatar asked Jun 25 '13 19:06

dbn


1 Answers

Looking through the source code for the subprocess module, it's because using a list of arguments with shell=True will do the equivalent of...

/bin/sh -c 'echo' '$ATESTVARIABLE'

...when what you want is...

/bin/sh -c 'echo $ATESTVARIABLE'

The following works for me...

import os, subprocess

os.environ['ATESTVARIABLE'] = 'value'
value = subprocess.check_output('echo $ATESTVARIABLE', shell=True)
assert 'value' in value

Update

FWIW, the difference between the two is that the first form...

/bin/sh -c 'echo' '$ATESTVARIABLE'

...will just call the shell's built-in echo with no parameters, and set $0 to the literal string '$ATESTVARIABLE', for example...

$ /bin/sh -c 'echo $0'
/bin/sh
$ /bin/sh -c 'echo $0' '$ATESTVARIABLE'
$ATESTVARIABLE

...whereas the second form...

/bin/sh -c 'echo $ATESTVARIABLE'

...will call the shell's built-in echo with a single parameter equal to the value of the environment variable ATESTVARIABLE.

like image 83
Aya Avatar answered Oct 30 '22 10:10

Aya