Python安装Mysql驱动出错解决(最新出炉)

Python中最连接Mysql常用的驱动是

mysql-python :mysql的C语言的驱动
mysql-connector:msql官方的驱动
pymysql:python语言的驱动

我这里安装的是 mysql-python

终端中执行

pip install mysql-python

运气好的的话,直接就成功了,我遇到了一下问题,最终找到了解决方案,原因及解决方案如下,供参考

问题1:mysql_config not found

终端打印出:

Collecting mysql-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 110kB 30kB/s 
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/private/tmp/pip-build-NP8J3v/mysql-python/	setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % 	(mysql_config.path,))
    EnvironmentError: mysql_config not found

----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-build-NP8J3v/mysql-python

解决方法,执行:

ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

原因
找不到mysql_config一般是由于通过lnmp.org或者其他方式安装mysql以后mysql_config是在/usr/local/mysql/bin/里面,这里面的文件不是在任意位置都可以访问的,而指令是

将mysql_config链接到/usr/local/bin目录下

参考一条命令解决mysql_config not found

问题2:image not found python

出错信息为:

Traceback (most recent call last):
  File "manage.py", line 4, in <module>
    from  models import User
  File "/Users/tengfei/PycharmProjects/mysql/models.py", line 1, in <module>
    import  MySQLdb
  File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: image not found

解决:

pip install --upgrade pip

修改 OS X 环境变量:打开终端,在终端中使用 vim 打开 “~/.bash_profile”,如果没有安装 vim,那就显示隐藏文件用文本编辑器打开,具体操作这里就不复述了。在 .bash_profile 中添加以下内容

PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes

其中 VERSIONER_PYTHON_PREFER_64_BIT 和 VERSIONER_PYTHON_PREFER_64_BIT 根据自己安装的 MySQL 进行选择。

参考:

MySQLdb(即 MySQL-python 包)在 OS X 中安装指南

Solving Library not loaded: libmysqlclient.18.dylib when importing MySQLdb on Lion

Mac OS + MySQL-python-1.2.3: ImportError | Library not loaded | libmysqlclient.16.dylib

问题3:command 'x86_64-linux-gnu-gcc' failed with exit status 1 异常解决

执行pip install mysql-python后的出错信息如下:
这是在虚拟环境中见到的
^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/home/tengfei/api01/test/TestTo/TODO-orm/venv/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-w6mp1W/mysql-python/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-jG7EKf-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/tengfei/api01/test/TestTo/TODO-orm/venv/include/site/python2.7/mysql-python" failed with error code 1 in /tmp/pip-build-w6mp1W/mysql-python/

原因是:一些依赖包没有安装,注意这也是很多实用pip执行安装扩展的时候,常见的错误

apt-get build-dep python-lxml

sudo pip install lxml --upgrade

pip install mysql-python

参考:[Ubuntu/pip] 解决 pip 安装 lxml 出现 x86_64-linux-gnu-gcc 异常

posted @ 2016-07-05 00:42  Darren.Von  阅读(37250)  评论(1编辑  收藏  举报
新浪微博:IT_攻城师,github:darren90(欢迎★star点赞)