Linux中的Python环境变量 [英] Environment Variables in Python on Linux

查看:112
本文介绍了Linux中的Python环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python访问环境变量不能准确反映操作系统对进程环境的看法。



os.getenv和os.environ不能像预期的那样运行



有没有办法正确获取正在运行的进程环境?






为了演示我的意思,请使用两个大致相同的程序(C中的第一个,python中的另一个):

  #include< stdio.h> 
#include< stdlib.h>
#include< unistd.h>
int main(int argc,char * argv []){
char * env;
for(;;){
env = getenv(SOME_VARIABLE);
if(env)
puts(env);
sleep(5);
}
}






  import os 
import time
while True:
env = os.getenv(SOME_VARIABLE)
如果env不是None:
print env
time.sleep(5)






现在,如果我们运行C程序,并使用gdb附加到正在运行的进程,并通过执行以下操作强制更改环境:

 (gdb)print setenv(SOME_VARIABLE,my value,1)
[切换到线程-1208600896(LWP 16163)]
$ 1 = 0
(gdb)print(char *)getenv(SOME_VARIABLE)
$ 2 = 0x8293126我的价值

然后上述C程序将开始每5秒发出一次我的价值。然而,上述的python程序不会。



在这种情况下,有没有办法让python程序像C程序一样运行?



(是的,我意识到这是一个非常晦涩和潜在的破坏性的行动,在正在运行的过程中执行)



另外,目前使用的是python 2.4,这可能已经在以后的python版本中修复了。

解决方案

这是一个很好的问题。 p>

事实证明, os 模块初始化 os.environ posix 的价值code> .environ ,它是在解释器启动时设置的。换句话说,标准库似乎没有提供对 getenv 函数。



这是一个可能会安全使用 ctypes 。因为你会调用超标准的libc函数。


Python's access to environment variables does not accurately reflect the operating system's view of the processes environment.

os.getenv and os.environ do not function as expected in particular cases.

Is there a way to properly get the running process' environment?


To demonstrate what I mean, take the two roughly equivalent programs (the first in C, the other in python):

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char *argv[]){
    char *env;
    for(;;){
        env = getenv("SOME_VARIABLE");
        if(env)
            puts(env);
        sleep(5);
    }
}


import os
import time
while True:
    env = os.getenv("SOME_VARIABLE")
    if env is not None:
        print env
    time.sleep(5)


Now, if we run the C program and attach to the running process with gdb and forcibly change the environment under the hood by doing something like this:

(gdb) print setenv("SOME_VARIABLE", "my value", 1)
[Switching to Thread -1208600896 (LWP 16163)]
$1 = 0
(gdb) print (char *)getenv("SOME_VARIABLE")
$2 = 0x8293126 "my value"

then the aforementioned C program will start spewing out "my value" once every 5 seconds. The aforementioned python program, however, will not.

Is there a way to get the python program to function like the C program in this case?

(Yes, I realize this is a very obscure and potentially damaging action to perform on a running process)

Also, I'm currently using python 2.4, this may have been fixed in a later version of python.

解决方案

That's a very good question.

It turns out that the os module initializes os.environ to the value of posix.environ, which is set on interpreter start up. In other words, the standard library does not appear to provide access to the getenv function.

That is a case where it would probably be safe to use ctypes on unix. Since you would be calling an ultra-standard libc function.

这篇关于Linux中的Python环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆