如何解决AttributeError:'_Environ'对象没有属性'has_key' [英] how to solve AttributeError: '_Environ' object has no attribute 'has_key'

查看:4345
本文介绍了如何解决AttributeError:'_Environ'对象没有属性'has_key'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'


推荐答案

我想你正在python 3.在Python 2中,字典有一个 has_key()方法。在Python 3中,正如异常所述,它不再存在。您需要使用运算符中的

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:

if 'SERVER_SOFTWARE' in os.environ

这里有一个例子(py3k):

here you have an example (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'
>>> 

这篇关于如何解决AttributeError:'_Environ'对象没有属性'has_key'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆