pip 安装 mysql-python 失败,并显示 EnvironmentError:找不到 mysql_config

(mysite)zjm1126@zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python
Downloading/unpacking mysql-python
  Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded
  Running setup.py egg_info for package mysql-python
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
      File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, 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 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 14, in <module>

  File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, 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 24, 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
Storing complete log in /home/zjm1126/.pip/pip.log
(mysite)zjm1126@zjm1126-G41MT-S2:~/zjm_test/mysite$ pip install mysql-python
Downloading/unpacking mysql-python
  Running setup.py egg_info for package mysql-python
    sh: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 14, in <module>
      File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, 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 24, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: mysql_config: not found

Traceback (most recent call last):

  File "<string>", line 14, in <module>

  File "/home/zjm1126/zjm_test/mysite/build/mysql-python/setup.py", line 15, 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 24, 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
Storing complete log in /home/zjm1126/.pip/pip.log

答案

看来您的系统上缺少 mysql_config 或安装程序找不到它。确保确实安装了 mysql_config。

例如,在 Debian / Ubuntu 上,您必须安装软件包:

sudo apt-get install libmysqlclient-dev

也许 mysql_config 不在您的路径中,当您自己编译 mysql 套件时就是这种情况。

更新:对于最新版本的 debian / ubuntu(截至 2018 年),它是

sudo apt install default-libmysqlclient-dev

运行 Mac OSX Mountain Lion,我只是在终端中运行此程序来修复:

export PATH=$PATH:/usr/local/mysql/bin

这是我找到的最快的修复程序 - 将其添加到路径中,但是如果您打算在其他环境中安装 MySQL-python,则最好将其永久添加(即,将其添加到/etc/paths )。

apt-get install libmysqlclient-dev python-dev

似乎做到了。

上面的问题可能有各种答案,下面是一个汇总的解决方案。

对于 Ubuntu:

$ sudo apt update
$ sudo apt install python-dev
$ sudo apt install python-MySQLdb

对于 CentOS:

$ yum install python-devel mysql-devel

如果您使用的是 MAC,请全局安装

brew install mysql

然后像这样导出路径

export PATH=$PATH:/usr/local/mysql/bin

最后在全球或您的静脉

pip install MySQL-Python

注意:全球范围内适用于 python3,因为 Mac 可以同时具有 python2 和 3

pip3 install MySQL-Python

您可以使用 MySQL Connector / Python

通过 PyPip 安装

pip install mysql-connector-python

可以在MySQL Connector / Python 1.0.5 beta 公告博客上找到更多信息。

在 Launchpad 上,有一个很好的示例,说明如何使用库添加,编辑或删除数据

对于centos用户:

yum install -y mysql-devel python-devel python-setuptools

然后

pip install MySQL-python


如果此解决方案不起作用 ,请打印 gcc 编译错误,例如:
_mysql.c:29:20: error: Python.h: No such file or directory

您需要指定Python.h的路径,如下所示:
pip install --global-option=build_ext --global-option="-I/usr/include/python2.6" MySQL-python

我试图在 Amazon EC2 Linux 实例上安装mysql-python ,但必须安装这些:

yum install mysql mysql-devel mysql-common mysql-libs gcc

但是后来我得到了这个错误:

_mysql.c:29:20: fatal error: Python.h: No such file or directory

所以我安装了:

yum install python-devel

那就成功了。

对于使用 MariaDB 而不是 MySQL 的任何人,解决方案是安装libmariadbclient-dev软件包,并使用正确的名称创建指向配置文件的符号链接。

例如,这对我有用:

ln -s /usr/bin/mariadb_config /usr/bin/mysql_config

OSX 小牛

由于 osx mavericks 和 xcode 开发工具中的更改,您可能会在安装时得到错误

clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

因此使用:

sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install mysql-python