python使用mysql的一些坑


注意:如果你用的是python3.x,直接去看第四個問題

遇到的第一個問題

  • 正常來說直接執行pip安裝,就是可以的,但是MySQL-python偏偏比較獨特

pip install MySQL-python
  • 報錯
_mysql.c:44:10: fatal error: 'my_config.h' file not found
    #include "my_config.h"
             ^~~~~~~~~~~~~
    1 error generated.
    error: command 'cc' failed with exit status 1

解決第一個問題

  • 執行brew install mysql-connector-c

brew install mysql-connector-c
  • 如果這一步直接完成,那就可以繼續pip install MySQL-python了,應該會成功
  • 但是我在這一步執行失敗了

遇到第二個問題

    • brew install mysql-connector-c報錯

Error: Cannot install mysql-connector-c because conflicting formulae are installed.
  mysql: because both install MySQL client libraries

Please `brew unlink mysql` before continuing.

解決第二個問題

  • 按照報錯的提示,執行brew unlink mysql
  • 沒有發生什么意外,執行完畢,繼續執行brew install mysql-connector-c
  • 'mysql-connector-c'安裝成功
  • 執行brew link --overwrite mysql,重新連接mysql(這一步我沒有做)
  • 然后再執行pip install MySQL-python,如果成功了就搞定了
  • 神奇的是,我在這一步又失敗了

遇到的第三個問題

  • 上面的步驟走完以后,執行pip install MySQL-python,報錯

Collecting mysql
  Downloading https://files.pythonhosted.org/packages/06/ef/c4efbf2a51fb46aba9be03a973638d9539c9ca10a5259b2cbb1a66133b2e/mysql-0.0.1.tar.gz
Collecting MySQL-python (from mysql)
  Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/zn/t8xxx4m149s9jqp1810ndrz80000gn/T/pip-install-oHMKPE/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
      File "setup_posix.py", line 53, in get_config
libraries = [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]
      File "setup_posix.py", line 8, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
    IndexError: string index out of range

解決第三個問題

    • 修改mysql的配置文件mysql_config,修改前記得cp一下 
      執行mysql_config,查看一下路徑
    • 打開文件vim mysql_config,找到libs="$libs -l ",改為libs="$libs -lmysqlclient -lssl -lcrypto "

libs="-L$pkglibdir"
# libs="$libs -l "                              # 原來的
libs="$libs -lmysqlclient -lssl -lcrypto "      # 更改后的
embedded_libs="-L$pkglibdir"
embedded_libs="$embedded_libs -l "
  • 再來一遍pip install MySQL-python
  • 終於成功了!可喜可賀!可喜可賀!
  • 小心翼翼的試一下,import MySQLdb,真的成功了

發現第四個問題


 

  • 開始使用的時候,發現自己用的是python2.x的環境,換成python3.x繼續用
  • import MySQLdb的時候又出問題了,ModuleNotFoundError: No module named 'MySQLdb'
  • 嘗試使用pip3 install MySQL-python再安裝一次,報錯

Collecting MySQL-python
  Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/setup.py", line 13, in <module>
from setup_posix import get_config
      File "/private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/setup_posix.py", line 2, in <module>
from ConfigParser import SafeConfigParser
    ModuleNotFoundError: No module named 'ConfigParser'

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/07/v8slhf9x5zsbbd8_9cd5ztnh0000gp/T/pip-install-oh_74ez5/MySQL-python/

解決第四個問題

  • 查到了原因,感到一陣陣的無語

In Python 3, ConfigParser has been renamed to configparser for PEP 8 compliance. It looks like the package you are installing does not support Python 3.
在Python3中,ConfigParser為了符合PEP8規范,已重命名為configparser。看起來你正在安裝的軟件包不支持Python3。

    • 因為不支持python3,建議使用pip install pymysql,安裝也沒那么多套路
    • 其實也找到了解決方案(沒有測試,我也不知道對不對,單純的記錄一下)

      • 方法一, 修改six模塊為

try:
    import configparser
except:
    from six.moves import configparser

方法二

cp /usr/local/lib/python3.7/configparser.py /usr/local/lib/python3.7/ConfigParser.py

最后是交流群887934385,探討技術,學習提升。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM