Python os.getenv 和 os.environ.get 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16924471/
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
Difference between os.getenv and os.environ.get
提问by André Staltz
Is there any difference at all between both approaches?
这两种方法有什么区别吗?
>>> os.getenv('TERM')
'xterm'
>>> os.environ.get('TERM')
'xterm'
>>> os.getenv('FOOBAR', "not found") == "not found"
True
>>> os.environ.get('FOOBAR', "not found") == "not found"
True
They seem to have the exact same functionality.
它们似乎具有完全相同的功能。
采纳答案by giwyni
One difference observed (Python27):
观察到的一个差异(Python27):
os.environraises an exception if the environmental variable does not exist.
os.getenvdoes not raise an exception, but returns None
os.environ如果环境变量不存在,则引发异常。
os.getenv不引发异常,但返回 None
回答by W. Conrad Walden
See this related thread. Basically, os.environis found on import, and os.getenvis a wrapper to os.environ.get, at least in CPython.
请参阅此相关线程。基本上,os.environ可以在导入时找到,并且os.getenv是 的包装器os.environ.get,至少在 CPython 中是这样。
EDIT: To respond to a comment, in CPython, os.getenvis basically a shortcut to os.environ.get; since os.environis loaded at import of os, and only then, the same holds for
os.getenv.
编辑:要回复评论,在 CPython 中,os.getenv基本上是os.environ.get; 因为os.environ是在 的导入时加载的os,只有在那时,同样适用于
os.getenv。
回答by Zulu
In Python 2.7 with iPython:
在带有 iPython 的 Python 2.7 中:
>>> import os
>>> os.getenv??
Signature: os.getenv(key, default=None)
Source:
def getenv(key, default=None):
"""Get an environment variable, return None if it doesn't exist.
The optional second argument can specify an alternate default."""
return environ.get(key, default)
File: ~/venv/lib/python2.7/os.py
Type: function
So we can conclude os.getenvis just a simple wrapper around os.environ.get.
所以我们可以得出结论,os.getenv它只是一个简单的包装器os.environ.get。
回答by fredrik
In addition to the answers above:
除了上面的答案:
$ python3 -m timeit -s 'import os' 'os.environ.get("TERM_PROGRAM")'
200000 loops, best of 5: 1.65 usec per loop
$ python3 -m timeit -s 'import os' 'os.getenv("TERM_PROGRAM")'
200000 loops, best of 5: 1.83 usec per loop
回答by user2357112 supports Monica
While there is no functional difference between os.environ.getand os.getenv, there is a massivedifference between os.putenvand setting entries on os.environ. os.putenvis broken, so you should default to os.environ.getsimply to avoid the way os.getenvencourages you to use os.putenvfor symmetry.
虽然os.environ.get和之间没有功能差异os.getenv,但和 上的设置条目之间存在巨大差异。被破坏,所以你应该默认为简单地避免鼓励你使用对称的方式。os.putenvos.environos.putenvos.environ.getos.getenvos.putenv
os.putenvchanges the actual OS-level environment variables, but in a way that doesn't show up through os.getenv, os.environ, or any other stdlib way of inspecting environment variables:
os.putenv改变实际的OS级的环境变量,但在某种程度上,它不是通过露面os.getenv,os.environ或检查环境变量的任何其他方式STDLIB:
>>> import os
>>> os.environ['asdf'] = 'fdsa'
>>> os.environ['asdf']
'fdsa'
>>> os.putenv('aaaa', 'bbbb')
>>> os.getenv('aaaa')
>>> os.environ.get('aaaa')
You'd probably have to make a ctypes call to the C-level getenvto see the real environment variables after calling os.putenv. (Launching a shell subprocess and asking it for its environment variables might work too, if you're very careful about escaping and --norc/--noprofile/anything else you need to do to avoid startup configuration, but it seems a lot harder to get right.)
你可能不得不做出一个ctypes打电话到C级getenv,看后叫真正的环境变量os.putenv。(启动一个 shell 子进程并询问它的环境变量也可能起作用,如果你非常小心地转义和--norc//--noprofile你需要做的任何其他事情来避免启动配置,但似乎很难做到正确。)

