剛學習python,到數據庫這塊,須要安裝一個MySQLdb。記錄下碰到的問題~~~
個人環境:Mac High Sierra 10.13.6 + python 2.7 + XAMPP。html
特此說明:如下安裝只針對本機以及本人碰到的問題,不同對任何人通用~~~本人也是裝了塊一成天才成功python
本機安裝了兩個版本的pyhton,以下。mysql
ygdeMacBook-Pro:MySQL-python-1.2.5 yg$ python --version Python 2.7.10 ygdeMacBook-Pro:MySQL-python-1.2.5 yg$ python3 --version Python 3.7.0 ygdeMacBook-Pro:MySQL-python-1.2.5 yg$
因爲我本機是已經安裝了xampp,不想再安裝mysql,在google找到以下這篇文章:sql
Install MySQL for python on Mac OS 10.9 with XAMPP ====>本篇文章主要參考!!!數據庫
這裏我主要列下安裝步驟macos
sudo python setup.py clean sudo python setup.py build sudo python setup.py install
這裏編譯還有可能出現異常vim
_mysql.c:44:10: fatal error: 'my_config.h' file not found
api
這是由於XAMPP等,若是是IDE安裝的話,沒有編譯環境,因此須要單獨安裝mysql-connector-c或者從新安裝mysqlbash
brew install mysql-connector-c
>>> import MySQLdb >>> MySQLdb.apilevel若是出現以下錯誤
ImportError: dlopen(/Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.9-intel.egg/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib學習
Referenced from: /Library/Python/2.7/site-packages/MySQL_python-1.2.4b4-py2.7-macosx-10.9-intel.egg/_mysql.so
Reason: image not found
This is because the mysqllibrary (of XAMPP) could not be found. You should make a link of mysql libray file to your /usr/lib :
sudo ln -s /Applications/XAMPP/xamppfiles/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
若是 出現 Operation not permitted ,咋整???
參考這個:python操做MySQL數據庫出錯Reason: unsafe use of relative rpath libmysqlclient.18.dylib
# /Users/yg/.python-eggs/MySQL_python-1.2.5-py2.7-macosx-10.13-intel.egg-tmp/_mysql.so
把上面這個文件_mysql.so貼到/Library/Python/2.7/site-packages/目錄下,而後執行以下命令便可
sudo install_name_tool -change libmysqlclient.18.dylib \ /Applications/XAMPP/xamppfiles/lib/libmysqlclient.18.dylib \ /Library/Python/2.7/site-packages/_mysql.so
參考:mac安裝mysqldb-Python
學習Python on mac©️操做mysql
OS X 10.11 下解決Python-MysqlDB沒法使用的問題
Python mysqldb: Library not loaded: libmysqlclient.18.dylib