python使用mysql的一些坑

时光不写博客 2019-07-29

python mysql shell string pip file brew

注意:如果你用的是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

参考链接:时光不写博客-_mysql.c:44:10:fatal error:'my_config.h'...

登录 后评论
下一篇
corcosa
13650人浏览
2019-10-08
相关推荐
【Python】模块之subprocess
618人浏览
2016-06-27 09:36:06
Python3操作MySql数据库
610人浏览
2017-10-12 23:18:48
0
0
0
307