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'
答案 0 :(得分:14)
我想你正在研究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'
>>>