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
在上面的代码中显示了以下错误
if os.environ.has_key('SERVER_SOFTWARE') \
AttributeError: '_Environ' object has no attribute 'has_key'
最佳答案
我假设您正在使用 Python 3。在 Python 2 中,字典有一个 has_key()
方法。在 Python 3 中,如异常所述,它不再存在。您需要使用 in
运算符:
if 'SERVER_SOFTWARE' in os.environ
这里有一个例子(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'
>>>
关于python - 如何解决AttributeError : '_Environ' object has no attribute 'has_key' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8427352/