windows7下python2.6 + mysql5.5(No module named MySQLdb/DLL load failed/from sets import ImmutableSet)

在使用python对MySQL数据库进行操作时,需要安装MySQLdb模块,而该模块的安装经常会遇到很多问题,下面是我在安装MySQLdb过程遇到问题的解决方法:

1.下载MySQLdb

MySQLdb版本: MySQL-python-1.2.2.win32-py2.6.exe 
下载地址:
http://home.netimperia.com/files/misc/MySQL-python-1.2.2.win32-py2.6.exe 
参见:
http://sourceforge.net/forum/forum.php?thread_id=2316047&forum_id=70460

2.安装完成后,在Python命令行下输入:import MySQLdb,会出现以下常见问题:

(1).ImportError: DLL load failed: 找不到指定的模块。

 
>>> import MySQLdb 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "D:\ProgramTools\Python2.6\Lib\site-packages\MySQLdb\__init__.py", line 19, in <module> 
    import _mysql 
ImportError: DLL load failed: 找不到指定的模块。 

解决方法:下载
libmmd.dll(附件)和libguide40.dll(附件)两个dll文件并复制System32目录之下;


(2).ImportError: DLL load failed: 找不到指定的模块。 

>>> import MySQLdb 
D:\ProgramTools\Python2.6\Lib\site-packages\MySQLdb\__init__.py:34: DeprecationWarning: the sets module is deprecated 
  from sets import ImmutableSet 

解决方法: 
1) 在文件 "__init__.py"(文件位置位于错误提示位置,如上面提示错误为D:\ProgramTools\Python2.6\Lib\site-packages\MySQLdb\__init__.py,即为需修改文件路径), 注释掉: 
from sets import ImmutableSet  
class DBAPISet(ImmutableSet):  
新增: 
class DBAPISet(frozenset)


2) 在文件中"converters.py", 注释掉  from sets import BaseSet, Set 这一句话。 
3) 在文件中"converters.py", 修改 "Set" 成为 "set" ( 只有两个地方需要修改): 
大概 line 48: return Set([ i for i in s.split(',') if i ]) ——> return set([ i for i in s.split(',') if i ]) 
大概 line 128: Set: Set2Str, ——> set: Set2Str 
参见:
http://sourceforge.net/forum/message.php?msg_id=5808948

经过以上两个步骤后,此时一般可以导入成功!


其他一些解决python MySQLdb错误的方法参见以下位置:

1.eclipse中无法导入mysqldb模块 http://berdy.iteye.com/blog/1106731

2.python数据库开发:http://jimi68.iteye.com/blog/387205

3. 折腾python 2.7 +django 1.2.5 +mysql 5.5 on windows xp了一晚上的记录,失败!

http://hi.baidu.com/lettoo/blog/item/6980f086b8223a2b67096ef7.html

原文地址:https://www.cnblogs.com/toxiaonan/p/3079491.html