【問題收錄】ImportError No module named MySQLdb 問題解決,importerrormysqldb

來源:互聯網
上載者:User

【問題收錄】ImportError No module named MySQLdb 問題解決,importerrormysqldb

1.查看是否已經安裝了MySQLdb 模組

進入python命令列,輸入import MySQLdb。如果沒有報錯,證明此模組已經安裝,否則我們需要進行一下幾部的操作。


2.安裝和下載MySQL for Python

MySQL for Python網址是http://sourceforge.net/projects/mysql-python/,可以下載安裝最新版本,命令如下

<span style="font-size:12px;">wget http://sourceforge.net/projects/mysql-python/files/latest/downloadtar -xvzf MySQL-python-1.2.4b4.tar.gzcd MySQL-python-1.2.4b4python setup.py buildpython setup.py install</span>


若在執行python setup.py build時報錯報EnvironmentError: mysql_config not found,需要進行如下操作:

使用whereis mysql_config命令,如果為出現下面的結果我們還需要安裝其他的東西:

我的mySQL是安裝在系統的預設路徑下

這個時候我們在安裝dev

apt-get install libmysqlclient15-dev

在執行一次whereis mysql_config
修改MySQL-python-1.2.4b4/site.cfg檔案,去掉mysql_config=XXX注釋,並改成mysql_config=/usr/bin/mysql_config

回到MySQLdb源碼目錄, vi setup_posix.py 或 vimsetup_posix.py開啟設定檔,找到mysql_config.path一行,把路徑改為mysql安裝路徑下bin目錄,即mysql_config.path ="/usr/local/mysql/bin/mysql_config"


再執行如下命令即可

python setup.py build

python setup.py install






相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.