{"id":988,"date":"2015-09-26T23:57:59","date_gmt":"2015-09-26T15:57:59","guid":{"rendered":"http:\/\/switch.linesno.com\/?p=988"},"modified":"2015-09-26T23:57:59","modified_gmt":"2015-09-26T15:57:59","slug":"importerror-no-module-named-mysqldb","status":"publish","type":"post","link":"http:\/\/switch.linesno.com\/?p=988","title":{"rendered":"ImportError: No module named MySQLdb"},"content":{"rendered":"<div id=\"sina_keyword_ad_area2\" class=\"articalContent   \">\n<div>\n<div>ImportError: No module named MySQLdb<\/div>\n<div>\u8be5\u9519\u8bef\u662f\u6e90\u4e8e\u6211\u4eec\u6ca1\u6709\u5b89\u88c5Python\u8fde\u63a5MySQL\u6240\u9700\u7684MySQLdb\u5e93\u800c\u5f15\u8d77\u3002<\/div>\n<div><\/div>\n<div>MySQL\u662f\u6700\u6d41\u884c\u7684\u5f00\u6e90\u6570\u636e\u5e93\u4e4b\u4e00\uff0c\u4f46\u5728Python\u6807\u51c6\u5e93\u4e2d\u5e76\u6ca1\u6709\u96c6\u6210MySQL\u63a5\u53e3\u7a0b\u5e8f\uff0cMySQLdb\u662f\u4e00\u4e2a\u7b2c\u4e09\u65b9\u5305\uff0c\u9700\u72ec\u7acb\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002Python\u8fde\u63a5MySQL\u7684\u5173\u952e\u4e4b\u5904\u5728\u4e8e\u8bbe\u7f6e\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5728\u8fde\u63a5\u6210\u529f\u4e4b\u540e\uff0c\u5176\u5b9e\u4e0d\u7ba1\u540e\u7aef\u662f\u4f55\u79cd\u6570\u636e\u5e93\uff0c\u5bf9DB-API\u5bf9\u8c61\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u8fdb\u884c\u64cd\u4f5c\u90fd\u662f\u4e00\u6837\u7684\u3002<\/div>\n<div>\u4e0b\u8f7d\u5730\u5740\uff1ahttp:\/\/sourceforge.net\/projects\/mysql-python\/<\/div>\n<div><\/div>\n<div>\u5bf9\u4e8eLinux\u6765\u8bf4\uff0c\u6709\u591a\u91cd\u5305\u7ba1\u7406\u7cfb\u7edf\u548c\u5b89\u88c5\u673a\u5236\u3002\u5982\u679c\u4f7f\u7528\u7684\u662f\u5305\u542b\u67d0\u79cd\u5305\u7ba1\u7406\u5668\u7684Linux\uff0c\u90a3\u4e48\u53ef\u4ee5\u5f88\u8f7b\u677e\u7684\u5b89\u88c5Python MySQLdb\u5e93\u3002<\/div>\n<div>Linux Fedora, CentOS\u7cfb\u7edf\uff1ayum install MySQL-python<\/div>\n<div>Linux Ubuntu\u64cd\u4f5c\u7cfb\u7edf\uff1aapt-get install python-mysqldb<\/div>\n<div><\/div>\n<div>\u4e0b\u9762\u7684\u4ee3\u7801\u6f14\u793a\u4e86\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u8868\uff0c\u63d2\u5165\u548c\u8bbf\u95ee\u6570\u636e\u7b49\u7b80\u5355\u64cd\u4f5c\uff1a<\/div>\n<div>#!\/usr\/bin\/env python<\/div>\n<div><\/div>\n<div>\n<div>\n<div>import MySQLdb<\/div>\n<div><\/div>\n<div>def main():<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/> print &#8216;*** Connecting to database&#8217;<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cxn=MySQLdb.connect(host=&#8217;192.168.1.108&#8242;, user=&#8217;root&#8217;, passwd=&#8217;123456&#8242;, db=&#8217;test&#8217;)<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>if not cxn:<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>print &#8216;ERROR: connection not supported, exiting&#8217;<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>return<\/div>\n<div><\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur=cxn.cursor()<\/div>\n<div><\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>print &#8216;*** Creating users table&#8217;<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur.execute(&#8216;CREATE TABLE users(login VARCHAR(8), uid INT)&#8217;)<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>print &#8216;*** Inserting some users&#8217;<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur.execute(&#8220;INSERT INTO users VALUES(&#8216;john&#8217;, 7000)&#8221;)<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur.execute(&#8220;INSERT INTO users VALUES(&#8216;jane&#8217;, 7001)&#8221;)<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur.execute(&#8220;INSERT INTO users VALUES(&#8216;bob&#8217;, 7200)&#8221;)<\/div>\n<div><\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>print &#8216;*** Search for users starting with j&#8217;<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur.execute(&#8220;SELECT * FROM users WHERE login LIKE &#8216;j%'&#8221;)<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>for data in cur.fetchall():<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>print &#8216;%s\\t%s&#8217; % data<\/div>\n<div><\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cur.close()<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cxn.commit()<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>cxn.close()<\/div>\n<div><\/div>\n<div>if __name__ == &#8216;__main__&#8217;:<\/div>\n<div>\u00a0<wbr \/> \u00a0<wbr \/>\u00a0<wbr \/>main()<\/div>\n<\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"articalList borderc\">\n<div class=\"listBg\">\n<div class=\"listTit\">\u6211\u7684\u66f4\u591a\u6587\u7ae0\uff1a<\/div>\n<div class=\"chooseOk\">\n<ul>\n<li><label><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_74a7e56e0101akg3.html\" target=\"_blank\">Python\u5b9e\u73b0FTP\u5ba2\u6237\u7aef\u4f20\u8f93\u6587\u4ef6<\/a><\/label>(2013-10-12 22:23:56)<\/li>\n<li><label><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_74a7e56e01017s64.html\" target=\"_blank\">QSqlDatabase:\u00a0QMYSQL\u00a0driver\u00a0not\u00a0loaded<\/a><\/label>(2013-05-24 10:08:26)<\/li>\n<li><label><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_74a7e56e010177l8.html\" target=\"_blank\">python\u5b9e\u73b0\u5e26\u9a8c\u8bc1\u7801\u7f51\u7ad9\u7684\u81ea\u52a8\u767b\u9646<\/a><\/label>(2013-04-25 21:15:35)<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>ImportError: No module named MySQLdb \u8be5\u9519\u8bef\u662f\u6e90\u4e8e\u6211\u4eec\u6ca1\u6709\u5b89\u88c5Python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"class_list":["post-988","post","type-post","status-publish","format-standard","hentry","category-server"],"_links":{"self":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=988"}],"version-history":[{"count":1,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/988\/revisions"}],"predecessor-version":[{"id":989,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=\/wp\/v2\/posts\/988\/revisions\/989"}],"wp:attachment":[{"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=988"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/switch.linesno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}