Mac pip安装mysql-python失败。

13

我从官网安装了MySQL和Workbench。我有一个Django项目,其requirements.txt文件如下:

Django==1.11.12
djangorestframework==3.8.2
django-cors-headers==2.2.0
drfdocs==0.0.11
mysql-python==1.2.5
django-rest-auth==0.9.3
django-allauth==0.35.0
nltk==3.2.5
django-extensions==2.0.7
pyparsing==2.2.0
pydot==1.2.4

当我运行时

pip install -r requirements.txt

我遇到了一个错误 mysql_config not found。为了解决这个问题,我运行了以下命令:
PATH=$PATH:/usr/local/mysql/bin

现在它抛出异常。
    _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

    ----------------------------------------
Command ""/Users/nitish/gitProjects/Vision Backlog/vb_env/bin/python" -u -c "import setuptools, tokenize;__file__='/private/var/folders/ql/_w2_rlvs2351pdcnzhn04sf40000gn/T/pip-install-M4ue9E/mysql-python/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /private/var/folders/ql/_w2_rlvs2351pdcnzhn04sf40000gn/T/pip-record-7OCzf1/install-record.txt --single-version-externally-managed --compile --install-headers "/Users/nitish/gitProjects/Vision Backlog/vb_env/include/site/python2.7/mysql-python"" failed with error code 1 in /private/var/folders/ql/_w2_rlvs2351pdcnzhn04sf40000gn/T/pip-install-M4ue9E/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/ql/_w2_rlvs2351pdcnzhn04sf40000gn/T/pip-install-X6b4rU/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
6个回答

39

2
非常感谢!我花了几分钟的时间在这个问题上挣扎,直到找到了你的答案 :) - the_marcelo_r
就是这样!MySQL版本5.7起了作用。(我不得不安装旧版的MySQL-Python 1.2.5) - fullStackChris
我使用了来自mariadb的my_config.h,并将其放置在/usr/include中。然后pip2 install mysql-python就可以完美运行了。 - Bart Mensfort

13
请尝试:
CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install -r requirements.txt

如果它不起作用: 首先安装mysql-connector-c
brew install mysql-connector-c 
pip install MySQL-python

如果您正在使用Windows:

https://dev.mysql.com/downloads/connector/c/

您还可以通过使用pip3进行安装来检查其发生了什么。

更新: 如果您使用的是Linux: 请打开/usr/local/bin/mysql_config

然后:

#Create options
Libs = "-L $ pkglibdir "
Libs = " $ libs   -l"

替换为:

#Create options
Libs = "- L $ pkglibdir"
Libs = "$ libs -lmysqlclient -lssl -lcrypto"

保存并重新安装mysql-python

pip uninstall mysql-python
pip install mysql-python

针对 Mac:

LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysql-python

应该会有所帮助。


我有Python 2.7和pip版本10.0.1。 - Thinker
1
我正在使用Mac,并运行这两个命令,但仍然出现错误(请参见更新后的问题)。 - Thinker
1
很高兴能帮助你! - user9008857
1
我之后还必须遵循这个:https://github.com/PyMySQL/mysqlclient-python/issues/169#issuecomment-299778504 - lakshayg
非常感谢!这是我见过唯一有效的评论。 LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysql-python - Alex

4
brew install mysql@5.7
export PATH="/usr/local/Cellar/mysql@5.7/5.7.29/bin:$PATH"
export LDFLAGS="-L/usr/local/Cellar/mysql@5.7/5.7.29/lib -L/usr/local/Cellar/openssl@1.1/1.1.1d/lib"
export CPPFLAGS="-I/usr/local/Cellar/mysql@5.7/5.7.29/include -I/usr/local/Cellar/openssl@1.1/1.1.1d/include"
/Users/tivanli/source/bk_project/pizza/venv/bin/pip install MySQL-python==1.2.5

1
在 Catalina 10.15.7 上运行得非常好,适用于我需要启动的旧 Python 项目。只需更新路径以反映当前安装的 MySQL(5.7.32)和 OpenSSL(1.1.1h)即可。 - Banjer

0

对我来说,这是:

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

0

即使安装了所有必需的软件包,仍然在Ubuntu上遇到此问题的人们可以按照以下步骤进行:

  • 从官方网址复制 my_config.h 源代码

https://dev.mysql.com/doc/dev/mysql-server/8.0.11/my__config_8h_source.html

然后在/usr/include/my_config.h中创建一个文件并粘贴它

sudo nano /usr/include/my_config.h

您可以使用Ctrl + Shift + V将内容粘贴到终端,具体取决于您的操作系统版本。

现在安装包,无论是您的版本还是无版本。

 pip install mysqlclient==1.3.7

您可以根据 SQL 版本从官方链接获取文件的源代码。

原始链接 - http://www.kombitz.com/2020/04/29/my_config-h-no-such-file-or-directory-on-centos-8/


-3

只需卸载您的Mysql,并安装MySql 5.7,问题就解决了!

如果您尝试了以上任何解决方案都没有成功,那么这意味着您的MySql版本不符合要求。我曾经认为是因为Python或PyCharm的原因,所以我使用了virtualenv和conda,但没有运气,所以当我将我的依赖项与我的朋友的计算机进行比较时,发现是MySql版本的问题。降级到5.7,一切正常。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接