python “ cannot import name OrderedDict”

摘要:
/usr/bin/envpython3fromcollectionimportOrderedDict以下是错误接收:Traceback:文件“File_1.py”,第2行,在importfile_2File“File_2中。皮,皮“,第2行,在fromcollectionsdetOrderedDictImportError:cannotimportnameOrderedDict然而,一些人指出,更新的版本(如3.1)不会出现此问题,OrderedDict最初是python 2.7版本的一个功能。后来,导入模式被更改,但在部署期间没有导入错误,但在发布和OrderedDi期间出现错误计算机断层扫描!唉,我终于意识到云上有多少python和django版本#python显示版本是2.6.8 python#django。VERSION display django版本是最新版本1.4。Django找不到所以我想回去。然而,即使python指向2.6,它仍然提示没有modulenameddjango。仍然找不到core.management。找到django的manage.py后的第一句话打开:#!

本地测试一切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"

  

免责声明:文章转载自《python “ cannot import name OrderedDict”》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇解决Flink消费Kafka信息,结果存储在Mysql的重复消费问题okhttp-utils的封装之okhttp的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Python接口测试入门

接口测试基础篇用几个简单的案例,带你轻松了解接口测试 首先了解一下接口测试的重要性: 接口测试有什么要求呢? 首先需要清晰的接口文档,标准如下: 接口名称 接口类型 输入参数 每个参数名; 每个参数类型; 每个参数业务含义; 每个是否可空; 每个字段长度(可选,一般需要提供,有严格要求的字段需特别注明); 输出参数 状态码; 提示信息; 每个参...

Ansible主机清单Inventory文件hosts

Ansible主机清单Inventory文件hosts 发表于 2017-05-14 | 分类于 运维相关 , Ansible | | 阅读次数 4638| 字数统计 1,442 | 阅读时长预计 6 Ansible 通过读取默认的主机清单配置,可以同时连接到多个远程主机上执行任务组和主机之间的关系通过 inventory 文件配置. 默认的...

Egret Engine(白鹭引擎)介绍及windows下安装

Egret Engine简要介绍-----   Egret Engine(白鹭引擎)【Egret Engine官网:http://www.egret-labs.org/】是一款使用TypeScript语言构建的开源免费的移动游戏引擎。白鹭引擎的核心定位是开放,高效,优雅。通过它,你可以快速地创建HTML5类型的移动游戏,也可以将游戏项目编译输出成为目标移动...

python飞机大战源码和素材

python飞机大战素材及代码已上传百度云,链接: https://pan.baidu.com/s/1ptZzCC5Z0tqySrw6p7gHsw 提取码: pqxn plane_main 1 importpygame 2 from plane_sprites import * 3 4 5 classPlaneGame(object): 6 "...

Python脚本获取参数的方式

Python脚本获取参数的方式 在运维过程中,常常需要自己写脚本,而python往往是最佳的选择。那么怎么获取python脚本的命令行参数呢,例如: python demo.py a b c 怎么获取命令行参数,a,b,c 呢?可以通过sys库里面的argv来实现, 例如: $ cat demo.py #-*- coding: UTF-8 -*- i...

安装的 Python 版本太多互相干扰?pyenv 建议了解一下。

写在之前 我们都知道现在的 Python 有 Python2 和 Python3,但是由于各种乱七八糟的原因导致这俩哥们要长期共存,荣辱与共,尴尬的是这哥俩的差异还比较大,在很多时候我们可能要同时用到它们,所以就要在 Python2 和 Python3 之间来回的无缝切换。 安装不同版本的 Python 我已经够累了,还要在不同版本的 Python 之间切...