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"

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM