2

I am using below python code to reset the environment variable http_proxy in Linux CentOS 6, but it is not unsetting the variable for the rest of the Python script.

Code:

 import os 
 print "Unsetting http..." 
 os.system("unset http_proxy") 
 os.system("echo $http_proxy") 
 print "http is reset"

Output:

Unsetting http...
http://web-proxy.xxxx.xxxxxxx.net:8080
http is reset
Process finished with exit code 0
  • @Kusalananda , I want to reset the system 'http_proxy' , to open an internal url using python automation script and then want to set the http_proxy back to run the external url. It is just to run my automation script I need to toggle between reset and set proxy – Sum Feb 20 '18 at 17:34
  • That makes it clearer, thanks. I have updated the text of your question with some of that information. – Kusalananda Feb 20 '18 at 17:36
8

Each invocation of os.system() runs in its own subshell, with its own fresh environment:

>>> import os
>>> os.system("echo $$")
97678
0
>>> os.system("echo $$")
97679
0

You are unsetting the http_proxy variable, but then your subshell has completed executing the command (to wit: unset), and terminates. You then start a new subshell with a new environment in which to run echo.

I believe what you are trying to do is del os.environ['http_proxy'], or os.environ.pop('http_proxy') if you want to ensure there is no http_proxy environment variable whether or not one previously existed:

$ export foo=bar
$ python2
Python 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.environ['foo']
'bar'
>>> del os.environ['foo']
>>> os.system('echo $foo')

0
4

Each call to os.system() is creating a separate process. Do echo $$ inside them both to see that the PID changes with each.

So your first os.system() spawns a new process which inside of itself unsets one variable. This doesn't effect the variables values in the process of the python script. Then the first spawned process exits and you spawn a new process. The second os.system() spawns this new process which inherits all of the environment variables from the python script - which haven't been touched. So of course the second spawned process sees the variable, because it was never unset in the process of the python script so it won't missing from any new child processes.

See https://stackoverflow.com/questions/3575165/what-is-the-correct-way-to-unset-a-linux-environment-variable-in-python for how to do this in python and http://docs.python.org/library/os.html#os.environ for the official docs.

  • Now if I want to set new http_proxy manually for the CentOS 6.6 , then what is the correct way to do it permanently. I am using CentOS 6.6 image inside Vmware Workstation. – Sum Feb 22 '18 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.