python2换python3 后报错 No module named ‘ConfigParser‘

在CentOS 6.4环境下,尝试安装MySQL-python时遇到ImportError,原因是Python3.x中ConfigParser模块已被重命名为configparser。解决方案包括复制configparser.py为ConfigParser.py或使用import as进行模块名更改。此问题涉及到Python版本兼容性和模块导入。

 

系统: CentOS-6.4-x86_64 

Python : Python 3.4.5 和 Python 3.5.2 

 

安装 MySQL-python ,结果出错:

ImportError: No module named 'ConfigParser'

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ry1be27_/mysql-python/

 

原因:

在 Python 3.x 版本后,ConfigParser.py 已经更名为 configparser.py 所以出错!

 

可以看看系统中的 python 命令 ,系统自带个 python 2.6。

[plain] view plain copy

  1. [root@centos]# ls /usr/bin/python*  
  2. /usr/bin/python  /usr/bin/python2  /usr/bin/python2.6  
  3. [root@centos]#  
  4. [root@centos]# python2.6 -V  
  5. Python 2.6.6  
  6. [root@centos]#  


 

解决方法:两个

一  找到文件 configparser ,更名为旧版本的名称 ConfigParser

cp /usr/local/Python34/lib/python3.4/configparser.py /usr/local/Python34/lib/python3.4/ConfigParser.py 

二  import  as 改名

 

try:
    import pymysql
except:
    pass
try:
    import  MySQLdb as pymysql
except:
    pass


try:
    from ConfigParser import ConfigParser
except:
    pass
try:
    from configparser import ConfigParser
except:
    pass

文章出处:http://www.cnblogs.com/aaron-agu/ 只有毅力和决心才能使人真正具有价值!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值