51

当我想在 osx 10.8 上安装 mysql-python 时遇到这个问题,它显示找不到“my_config.h”文件的错误。

下面是我的运行代码:

sudo easy_install mysql-python
Password:
Searching for mysql-python
Reading http://pypi.python.org/simple/mysql-python/
Reading http://sourceforge.net/projects/mysql-python/
Reading http://sourceforge.net/projects/mysql-python
Best match: MySQL-python 1.2.3
Downloading http://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.3.tar.gz#md5=215eddb6d853f6f4be5b4afc4154292f
Processing MySQL-python-1.2.3.tar.gz
Running MySQL-python-1.2.3/setup.py -q bdist_egg --dist-dir /tmp/easy_install-UoZlf7/MySQL-python-1.2.3/egg-dist-tmp-phaQqE
warning: no files found matching 'MANIFEST'
warning: no files found matching 'ChangeLog'
warning: no files found matching 'GPL'
clang: warning: argument unused during compilation: '-mno-fused-madd'
_mysql.c:36:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
         ^
1 error generated.
error: Setup script exited with error: command 'clang' failed with exit status 1
ninjaiimac:MySQL-python-1.2.3 sunninjaisun$ 
4

13 回答 13

86
brew install mysql
brew unlink mysql
brew install mysql-connector-c
sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/bin/mysql_config
pip install MySQL-python
brew unlink mysql-connector-c
brew link --overwrite mysql
于 2018-07-23T16:59:30.360 回答
39

在 macOS High Sierra 上运行良好:

如果你还没有安装 mysql:brew install mysql

brew unlink mysql
brew install mysql-connector-c
brew link --overwrite mysql
pip install MySQL-python
于 2018-08-07T14:49:55.247 回答
28

请试试:

brew install mysql-connector-c 
pip install MySQL-python
于 2015-05-11T08:20:16.533 回答
13

我曾经brew在 Mac OS 10.14.2 上安装所有东西。

virtualenv当在 Mojave 上的虚拟环境 () 中时,其他答案对我不起作用。我按照 Jofsey 的指示进行操作,但出现错误:

#        define SIZEOF_LONG             4
                    ^
    In file included from _mysql.c:44:
    /usr/local/include/my_config.h:179:9: warning: 'SIZEOF_TIME_T' macro redefined [-Wmacro-redefined]
    #define SIZEOF_TIME_T    8
            ^
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pymacconfig.h:57:17: note: previous definition is here
    #        define SIZEOF_TIME_T           4
                    ^
    2 warnings generated.
    _mysql.c:287:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                    cmd_argc = PySequence_Size(cmd_args);
                             ~ ^~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:317:12: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                    groupc = PySequence_Size(groups);
                           ~ ^~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:470:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                            int j, n2=PySequence_Size(fun);
                                   ~~ ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:1127:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_real_escape_string(&(self->connection), out, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1129:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_escape_string(out, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1168:9: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
            size = PyString_GET_SIZE(s);
                 ~ ^~~~~~~~~~~~~~~~~~~~
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/stringobject.h:92:32: note: expanded from macro 'PyString_GET_SIZE'
    #define PyString_GET_SIZE(op)  Py_SIZE(op)
                                   ^~~~~~~~~~~
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/object.h:116:56: note: expanded from macro 'Py_SIZE'
    #define Py_SIZE(ob)             (((PyVarObject*)(ob))->ob_size)
                                     ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
    _mysql.c:1178:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_real_escape_string(&(self->connection), out+1, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1180:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                    len = mysql_escape_string(out+1, in, size);
                        ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:1274:11: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
            if ((n = PyObject_Length(o)) == -1) goto error;
                   ~ ^~~~~~~~~~~~~~~~~~
    /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/abstract.h:434:25: note: expanded from macro 'PyObject_Length'
    #define PyObject_Length PyObject_Size
                            ^
    _mysql.c:1466:10: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                            len = strlen(buf);
                                ~ ^~~~~~~~~~~
    _mysql.c:1468:10: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                            len = strlen(buf);
                                ~ ^~~~~~~~~~~
    _mysql.c:1504:11: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                                    len = strlen(buf);
                                        ~ ^~~~~~~~~~~
    _mysql.c:1506:11: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                                    len = strlen(buf);
                                        ~ ^~~~~~~~~~~
    13 warnings generated.
    cc -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.14-intel-2.7/_mysql.o -L/usr/local/Cellar/mysql-connector-c/6.1.11/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-intel-2.7/_mysql.so
    ld: library not found for -lssl
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command 'cc' failed with exit status 1

因此,在我运行了这些命令之后,我将其作为最终命令运行并且它起作用了:

sudo pip install MySQL-Python --global-option=build_ext --global-option="-I/usr/local/opt/openssl/include" --global-option="-L/usr/local/opt/openssl/lib"

编辑:

我在GitHub 上找到的另一个解决方案,假设您已经运行brew install openssl(这避免了对 CLI 参数的需要,并将防止未来的 SSL 库错误):

export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

您可以将上述内容放入您的~/.bash_profile文件中然后运行source ~/.bash_profile,或者如果它是虚拟环境,您可以找到一种方法在虚拟环境 shell 中运行导出并使用 . 检查虚拟环境 CLI 中的值echo $LIBRARY_PATH$LIBRARY_PATH设置后,您将不会继续看到这些 SSL 库错误。

于 2019-01-07T17:28:03.467 回答
10

我的问题是我使用的是 MAMP 的 mysql 版本,它不包含开发标头。我下载了官方版本的 dmg 并立即运行。

于 2012-09-02T03:14:09.147 回答
9

我检查了include这两个文件夹mysql并由mysql-connector-c自制软件安装,并惊讶地发现没有名为my_config.h. 因此,我认为现有的高票答案不再能解决问题。

这是我的解决方案mysql 8.0.19。我复制了我安装的文件夹mysql.h下的文件并将其保存在同一文件夹下。然后我按照下面的高票解决方案并成功安装。include/mysql-connector-cmy_config.hmysql-python 1.2.5

brew install mysql
brew unlink mysql
brew install mysql-connector-c   # modify the include/ folder after installing the package
sed -i -e 's/libs="$libs -l "/libs="$libs -lmysqlclient -lssl -lcrypto"/g' /usr/local/Cellar/mysql/8.0.19/bin/mysql_config
pip install MySQL-python
brew unlink mysql-connector-c
brew link --overwrite mysql

目前只知道可以成功安装包,但不确定是否是安全的解决方案。

实际上,mysql-python是一个相当老的包(最新版本是 6 年前),我推荐其他包用于在 python 中与 mysql 交互(例如mysql-connector-python)。

于 2020-05-14T14:43:17.857 回答
6

在 macos 上,我按照gethue的安装说明进行操作。然后我从Oracle下载了 my_config.h ,将文件放在 /usr/local/include 中,安装就可以继续了。但停在一个不相关的丢失文件上。

Oracle 的链接已更新为更新版本的 MySQL 8.0.26。

于 2020-01-27T08:32:49.330 回答
4

这在 macOS Catalina 10.15.7 和 python 2.7 上对我有用

brew install mysql@5.7
brew link --force mysql@5.7
PATH="/usr/local/opt/mysql@5.7/bin:$PATH" LDFLAGS="-L/usr/local/Cellar/mysql@5.7/5.7.29/lib -L/usr/local/Cellar/openssl@1.1/1.1.1d/lib" CPPFLAGS="-I/usr/local/Cellar/mysql@5.7/5.7.29/include -I/usr/local/Cellar/openssl@1.1/1.1.1d/include" pip install mysql-python
于 2020-12-10T11:58:01.287 回答
2

我从答案一路尝试,这对我不起作用。

  1. 只需从 https://dev.mysql.com/downloads/mysql/下载 mysql 并安装在您的 Mac 中。不使用 brew install mysql

  2. 安装 MySQL-python

    pip install MySQL-python  
    

做的每一件事

于 2020-05-14T07:56:41.813 回答
2

2020 年 11 月,Catalina 10.15.7Python 2.7.16对我有用的是两个答案的组合:

第 1 步:https ://stackoverflow.com/a/61800247/14686220

但是,我没有像在目录中那样简单地复制mysql.h文件(这可能很危险),而是从官方 MySQL 文档中复制了文件my_config.hinclude/my_config.h

第 2 步:https ://stackoverflow.com/a/54079052/14686220

于 2020-11-22T13:13:08.560 回答
0

如前所述,您需要 MySQL 的 dev 标头,默认情况下 MAMP 不附带这些标头。可以将标头添加到 MAMP 版本,而不是使用两个 MySQL 实例。这里有很好的说明:http: //dreamconception.com/tech/how-to-install-mysqldb-mysql-python-on-mamp/

确保不要只是复制粘贴所有命令,它们有点过时,因此您需要更改其中一些命令的版本号。

我正在使用 OS X 10.9 和 python 2.7,一切正常。

于 2013-12-14T15:45:25.817 回答
0

在 macOS 10.15 上,只要您的项目与 PyMySQL 1.3.11 或更高版本兼容,以下似乎可以解决此问题:

brew install mariadb-connector-c
pip install MySQL-python

参考:https ://github.com/PyMySQL/mysqlclient-python/blob/master/HISTORY.rst

于 2020-04-30T22:03:17.117 回答
0

在 macOS Catalina 10.15.5 上使用conda而不是pip为我解决了这个问题

conda install mysql-python
于 2020-07-16T11:35:31.390 回答