4

Possible Duplicate:
Is it possible to change the Environment of a parent process in python?

I am using python 2.4.3. I tried to set my http_proxy variable. Please see the below example and please let me know what is wrong. the variable is set according to python, however when i get out of the interactive mode. The http_proxy variable is still not set. I have tried it in a script and also tried it with other variables but i get the same result. No variable is actually set up in the OS.

Python 2.4.3 (#1, May  1 2012, 13:52:57)
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['http_proxy']="abcd"
>>> os.system("echo $http_proxy")
abcd
0
>>> print os.environ['http_proxy']
abcd
>>>
user@host~$ echo $http_proxy

user@host~$
Community
  • 1
  • 1
user1970157
  • 41
  • 1
  • 1
  • 4
  • 5
    Setting variables inside a Python script / shell doesn't affect the environment once the program stops running - it only affects the environment for the program and for things that are children of the program. – wkl Jan 11 '13 at 13:56
  • 2
    I think the environmental flag only applies to child processes. – user176581 Jan 11 '13 at 13:56
  • 1
    Env variables are automatically transported from super to sub shells, not the other way round. On unix, there is the `export` program that can do this imho. – Niklas R Jan 11 '13 at 13:57
  • 2
    http://stackoverflow.com/questions/263005/is-it-possible-to-change-the-environment-of-a-parent-process-in-python – alex vasi Jan 11 '13 at 13:58
  • 1
    lol, imho should be afaik xD – Niklas R Jan 11 '13 at 14:26

2 Answers2

3

When you run this code, you set the environment variables, its working scope is only within the process. After you exit (exit the interactive mode of python), these environment will be disappear.

As your code "os.system("echo $http_proxy")" indicates, if you want to use these environment variables, you need run external program within the process. These variables will be transfer into the child processes and can be used by them.

Wu Li
  • 789
  • 5
  • 6
0

environment variables are not a "global database of settings"; setting the environment here doesn't have any effect there.

the exception to this is that programs which invoke other programs can provide a different environment to their child programs.

At the shell, when you type

[~/]$ FOO=bar baz

you're telling the shell to invoke the program baz with some extra environment FOO.

You can do this in python, too, but changing os.environ will not have any effect. that variable only contains a regular python dict with whatever environment it was started with. You can change the environment python will use by passing an alternate value for env to subprocess.Popen

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • So is the way to get around this is ? I am trying to set http_proxy variable within the program and make outbound http requests from the same program – user1970157 Jan 14 '13 at 14:59