eclipse 如何解决 AttributeError: '_Environ' 对象没有属性 'has_key'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8427352/
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
how to solve AttributeError: '_Environ' object has no attribute 'has_key'
提问by anbu jeremiah
def _is_dev_mode():
# quick hack to check if the program is running in dev mode.
# if 'has_key' in os.environ
if os.environ.has_key('SERVER_SOFTWARE') \
or os.environ.has_key('PHP_FCGI_CHILDREN') \
or 'fcgi' in sys.argv or 'fastcgi' in sys.argv \
or 'mod_wsgi' in sys.argv:
return False
return True
in above code following error is shown
在上面的代码中显示以下错误
if os.environ.has_key('SERVER_SOFTWARE') \
AttributeError: '_Environ' object has no attribute 'has_key'
回答by joaquin
I supose you are working on python 3. In Python 2, dictionaries had a has_key()
method. In Python 3, as the exception says, it no longer exists. You need to use the in
operator:
我假设您正在使用 Python 3。在 Python 2 中,字典有一个has_key()
方法。在 Python 3 中,正如异常所说,它不再存在。您需要使用in
运算符:
if 'SERVER_SOFTWARE' in os.environ
here you have an example (py3k):
这里有一个例子(py3k):
>>> import os
>>> if 'PROCESSOR_LEVEL' in os.environ: print(os.environ['PROCESSOR_LEVEL'])
6
>>> if os.environ.has_key('PROCESSOR_LEVEL'): print("fail")
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
if os.environ.has_key('PROCESSOR_LEVEL'): print("fail")
AttributeError: '_Environ' object has no attribute 'has_key'
>>>