zoukankan      html  css  js  c++  java
  • python “ cannot import name OrderedDict”

    本地测试一切ok,结果放到亚马逊云上去,运行出现错误:“ cannot import name OrderedDict”,于是找到错误根源,在于我在某处用到了OrderedDict, 在头部“from collections import OrderedDict”, 

    google有人指出“from ## import ##”不支持二次导入,如果包含该语句的py文件再次被导入就会出现error。真是奇特!

    Cannot use "from X import Y"
    Issue #87 created 2010-09-07
    You need two files, file_1.py and file_2.py, if one file imports the other and the other uses the import method of "from X import Y" then there will be an unexpected import error, an error that as you can imagine is absent from running the program normally.
    
    First file:
    
    #!/usr/bin/env python3
    import file_2
    Second file:
    
    #!/usr/bin/env python3
    from collections import OrderedDict
    Here is the error I receive:
    
    Traceback (most recent call last):
      File "file_1.py", line 2, in <module>
        import file_2
      File "file_2.py", line 2, in <module>
        from collections import OrderedDict
    ImportError: cannot import name OrderedDict

    但是也有人指出更新的版本如3.1不会出现这个问题,而且OrderedDict这个东西本来是2.7版本的python才有的特性。

    后来改了import方式,但是部署的时候不会出现导入error,却在执行post,走到OrderedDict时error了!哎,最后才想到云上的python和django版本到底是多少?!查一下!

    # python
    显示版本为2.6.8
    python # django.VERSION
    显示django版本为1.4  最新版本。

    好吧,那就升级python版本(后来我知道,其实可以easy_install 安装OrderedDict的package),可惜我找了个源代码编译安装版本的,链接为:

    http://www.weilife.org/work-share/technical-experience/centos%E4%B8%ADpython%E5%8D%87%E7%BA%A7%E5%88%B02-7%E6%95%99%E7%A8%8B

    装好了2.7版本才发现原来的库完全不能用了啊!django找不到了...于是又想回退...可是即使python指向2.6了还是提示

    no module named django.core.management

    还是没找到,定位到django的manage.py 打开后第一句话:

    #!/usr/bin/env python

    在linux终端下面直接输入 :

    /usr/bin/env python  执行结果显示:指向版本2.7!  啊啊啊啊,怎么能这样嗯?好吧,咋卸载嗯?卸载了估计会将2.6影响到不能用吧?算了,还是在2.7的基础上装上需要用的库吧!
    安装顺序: easy_install ;django; mysqlDB
    期待明天继续折腾。。。。




    补序:由于部署的环境是python2.6,还不能升级到python2.7, 最终问题还是要解决,回来修这个bug.
    给要部署的服务器打补丁:第三方插件ordereddict
    easy_install
    ordereddict 或者 pypi install ordereddict
    网址:   http://pypi.python.org/pypi/ordereddict)



    安装成功以后, 修改代码如下:

    try:
        from collections import OrderedDict as  ordict
    except ImportError:
        try:
            import ordereddict as ordict
        except ImportError:
            raise
    
    
    
    
    def iniMonitorItemsByServerType(serverType):
        try:
            conn = connectToDataBase()
            cur = conn.cursor(db.cursors.DictCursor)
            selectSQL = """select * from config_server_type where server_type = %s """%(str(serverType))
            count = cur.execute(selectSQL)
            row = cur.fetchone()
            for key in row.keys():
                if row.get(key)==0:
                    row.pop(key)
                # use OrderedDict to set index of iframes in the monitoring
            monitorItems = ordict()
            alist = ['Uptime','RPS', 'Resp_time','CPU','Mem','Disk','Network','Queue_length','Pool_size']
            for key in alist:
                if row.has_key(key):
                    monitorItems[key] = row.get(key)
            return monitorItems
    
    
        except Exception:
            print "error in iniMonitorItemsByServerType"
    

      

  • 相关阅读:
    day31-python之内置函数
    day30-python之socket
    day28-python之property
    day27-python之迭代器协议
    day26-python之封装
    day25-python之继承组合
    初识AJAX
    写博客的心得
    web前端常见面试题
    学习网络安全的网站
  • 原文地址:https://www.cnblogs.com/xiami303/p/2660657.html
Copyright © 2011-2022 走看看