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 fromrunning the program normally.First file:#!/usr/bin/env python3 importfile_2 Second file:#!/usr/bin/env python3 from collections importOrderedDict Hereisthe error I receive:Traceback (most recent call last):File"file_1.py", line 2, in <module>importfile_2File"file_2.py", line 2, in <module>from collections importOrderedDict ImportError: cannotimport name OrderedDict
但是也有人指出更新的版本如3.1不会出现这个问题,而且OrderedDict这个东西本来是2.7版本的python才有的特性。
后来改了import方式,但是部署的时候不会出现导入error,却在执行post,走到OrderedDict时error了!哎,最后才想到云上的python和django版本到底是多少?!查一下!
#python 显示版本为2.6.8python#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.给要部署的服务器打补丁:第三方插件ordereddicteasy_install ordereddict 或者 pypi install ordereddict
网址: http://pypi.python.org/pypi/ordereddict) 安装成功以后, 修改代码如下:
try:from collections import OrderedDict as ordict
except ImportError:try:import ordereddict as ordictexcept ImportError:raisedef 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 monitoringmonitorItems = 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 monitorItemsexcept Exception:print "error in iniMonitorItemsByServerType"
转载于:https://www.cnblogs.com/xiami303/archive/2012/08/28/2660657.html
python “ cannot import name OrderedDict”相关推荐
- 报错 之 from typing import OrderedDict ImportError: cannot import name ‘OrderedDict‘ from ‘typing‘
from typing import OrderedDict ImportError: cannot import name 'OrderedDict' from 'typing' (E:\Pytho ...
- ImportError: cannot import name ‘OrderedDict‘ from ‘typing‘的解决办法
from torchvision import datasets, transforms 报了 ImportError: cannot import name 'OrderedDict' from ' ...
- linux rpm找不到命令_linux环境下 python环境import找不到自定义的模块
linux环境下 python环境import找不到自定义的模块 问题现象: Linux环境中自定义的模块swport,import swport 出错.swport模块在/root/sw/目录下. ...
- python的import与from...import的不同之处
在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相 应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序 ...
- Python中import导入上一级目录模块及循环import问题的解决
Python中import导入上一级目录模块及循环import问题的解决 参考文章: (1)Python中import导入上一级目录模块及循环import问题的解决 (2)https://www.cn ...
- python怎么导入时间-Python的import导入与时间
一.模块与包 模块,在Python可理解为对应于一个文件.在创建了一个脚本文件后,定义了某些函数和变量.你在其他需要这些功能的文件中,导入这模块,就可重用这些函数和变量.一般用module_name. ...
- python importerror怎么解决-解决python有时候import不了当前的包问题
问题描述: ImportError: No module named "XXXX' 解决方式一: 将XXXX包放在python的site-package文件夹下 解决方式二: 可能是XXXX ...
- python导入本地文件-Python使用import导入本地脚本及导入模块的技巧总结
本文实例讲述了Python使用import导入本地脚本及导入模块的技巧.分享给大家供大家参考,具体如下: 导入本地脚本 import 如果你要导入的 Python 脚本与当前脚本位于同一个目录下,只需 ...
- 一文搞懂 Python 的 import 机制
一.前言 希望能够让读者一文搞懂 Python 的 import 机制 1.什么是 import 机制? 通常来讲,在一段 Python 代码中去执行引用另一个模块中的代码,就需要使用 Python ...
最新文章
- SAP MM 事务代码MI31之思考之续集
- XenServer中Fast Copy与Full Copy的区别
- LPC55S69 MicroPython模组和库函数
- 树莓派:外设开发编程,控制继电器
- ionic.css界面组件:表单-复选按钮
- C++之路进阶codevs1242(布局)
- 嵌入式驱动开发学习路线
- Unity学习01-unity物体移动三种方式
- 项目风险管理之风险分析
- 云南 贵州大旱灾情百年一遇 经济损失惨重
- php下载MP4视频
- 改进duilib的richedit控件的部分功能
- c语言判断获取位置字符,C语言 · 判定字符位置
- Android SDK包下载分享
- 华夏基金:基金行业数字化转型实践成果分享
- 输入一个整数,判断是奇数还是偶数?
- 【项目管理】项目中的进度管理,你知道多少?
- 【程序设计】散点密度图
- git版本管理-代码提交时忽略target文件夹
- 第十三章 使用动态SQL(七)