python os.environ, os.putenv, /usr/bin/env
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17705419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python os.environ, os.putenv, /usr/bin/env
提问by CSJ
I want to ensure os.system('env')
not contain some specific variable myname
which is export in ~/.bashrc
as export myname=csj
我想确保os.system('env')
不包含某些myname
导出~/.bashrc
为的特定变量export myname=csj
Therefore, I wrote below python code:
因此,我写了下面的python代码:
import os
def print_all():
print "os.environ['myname']=%s" % os.environ.get('myname')
print "os.getenv('myname')=%s" % os.getenv('myname')
os.system('env | grep myname')
print
def delete_myname():
if 'myname' in os.environ: os.environ.pop('myname')
if os.getenv('myname'): os.unsetenv('myname')
print_all()
os.putenv('myname', 'csj2')
print "---------------------"
delete_myname()
print_all()
os.putenv('myname', 'csj3')
print "---------------------"
delete_myname()
print_all()
I think examine both os.environ['myname']
and os.getenv('myname')
and then delete them if exist,
can ensure os.system('env | grep myname')
get nothing.
我认为检查两者os.environ['myname']
,os.getenv('myname')
然后删除它们(如果存在),可以确保os.system('env | grep myname')
一无所获。
However, the result is:
然而,结果是:
os.environ['myname']=csj
os.getenv('myname')=csj
myname=csj
---------------------
os.environ['myname']=None
os.getenv('myname')=None
---------------------
os.environ['myname']=None
os.getenv('myname')=None
myname=csj3
I don't understand why I still got csj3
on os.system('env | grep myname')
?
我不明白为什么我还在csj3
继续os.system('env | grep myname')
?
采纳答案by mata
From the docs:
从文档:
Note:Calling putenv() directly does not change os.environ, so it's better to modify os.environ.
注意:直接调用putenv()不会改变os.environ,所以最好修改os.environ。
For unsetenv
there is a similar warining:
因为unsetenv
有一个类似的警告:
however, calls to unsetenv() don't update os.environ, so it is actually preferable to delete items of os.environ.
但是,对 unsetenv() 的调用不会更新 os.environ,因此实际上最好删除 os.environ 的项目。
getenv
just returns the value from os.environ
, as it's implementation shows, so by using it you get into a state where it seems the value isn't set when you look it up from python, while it acutally is in the real environment. The only way to get it now I can think of would be to call the c getenv function using ctypes...
getenv
只是从 返回值os.environ
,正如它的实现所显示的那样,因此通过使用它,您会进入一种状态,当您从 python 中查找该值时,它似乎没有设置该值,而它实际上是在真实环境中。我现在能想到的唯一方法是使用 ctypes 调用 c getenv 函数...
If i modify your code to use os.environ
isnstead of calling putenv
/unsetenv
everything works as expected:
如果我修改您的代码以使用os.environ
is而不是调用putenv
/unsetenv
一切按预期工作:
import os
def print_all():
print "os.environ['myname']=%s" % (os.environ['myname'] if 'myname' in os.environ else "None")
os.system('env | grep myname')
print
def delete_myname():
if 'myname' in os.environ: os.environ.pop('myname')
print_all()
os.environ['myname'] = 'csj2'
print "---------------------"
print_all()
delete_myname()
print_all()
os.environ['myname'] = 'csj3'
print "---------------------"
print_all()
delete_myname()
print_all()
output:
输出:
$ myname=somevalue python2 test.py
os.environ['myname']=somevalue
myname=somevalue
---------------------
os.environ['myname']=csj2
myname=csj2
os.environ['myname']=None
---------------------
os.environ['myname']=csj3
myname=csj3
os.environ['myname']=None
回答by Laurent LAPORTE
A good practice could be:
一个好的做法可能是:
- delete the
myname
environment variable (if it exists), - run your function
- restore the
myname
environment variable at function completion.
- 删除
myname
环境变量(如果存在), - 运行你的函数
myname
在函数完成时恢复环境变量。
Yu can do that easily with something like the modified_environ
context manager describe in this question.
Yu 可以使用类似modified_environ
上下文管理器在这个问题中描述的东西轻松做到这一点。
with modified_environ('myname'):
call_my_function()