无法在MacOS 10.12.4上使用pip安装mysql-python

9

运行pip install mysql-python时出现以下错误:

Collecting mysql-python
  Using cached 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/b_/xh05hxwd0lzc3rm858jsypdc0000gn/T/pip-build-AWDYaP/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

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/b_/xh05hxwd0lzc3rm858jsypdc0000gn/T/pip-build-AWDYaP/mysql-python/

我按照这篇关于重新安装setuptools的帖子中的说明进行了操作,而且我已经从https://www.python.org/重新安装了Python。

which -a python返回:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/local/bin/python
/usr/bin/python

看起来这个错误是在安装了MacOS 10.12.4更新后发生的。

7个回答

29

用这个解决了

brew install mysql
pip install MySQL-python

3
谢谢你的解决方案,这个问题让我很烦恼。实际上,我必须执行你和另一个人的方法:brew unlink mysql-connector-c,然后brew install mysql。 - Stan Lin
在此之后添加 brew link mysql - chadlagore
2
我确保已安装并链接了mysql,然后运行pip install MySQL-python,但仍然出现相同的错误。 - MhmdRizk

14

如果您遇到类似于“Failed building wheel for mysql-python”的错误,请尝试以下步骤:

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

这对我来说非常有效。


谢谢!你帮我省了很多时间! - mkaran
非常好!非常感谢您!也节省了我几个小时的时间 :) - Ugur
这是唯一对我有效的解决方案,非常感谢!! - Ghada
救了我的一天。 - ErenL
也拯救了我的毕业项目最终成绩。 - Jesus Hedo

12

这对我有效(macOS Mojave 10.14.2):

brew install mysql@5.7
brew link --force mysql@5.7
brew install mysql-client
LDFLAGS=-L/usr/local/opt/openssl/lib pip install mysql-python

2
谢谢!这对我很有帮助。而且还涉及到 Mojave 的处理。 - adamc
1
谢谢,省了我很多时间。在macOS Mojave上运行良好。 - Andreas Zwerger

4

如果你和我一样在 Mac 上从源代码安装了 MySQL,并忘记了建议使用 brew 的人 - 你的机器已经有了 mysql-connector-c。如果你执行了 brew install mysql-connector-c,请执行 brew uninstall mysql-connector-c,然后再次尝试 pip install mysql-python(针对 Python 3)。


3
这是我的解决方案:

这对我很有效:

    brew install mysql@5.7
    brew link --force mysql@5.7

1
为了解决这个问题,我们需要编辑mysql_config文件。
为此,请执行以下命令:
vi `which mysql_config`

这将在vi编辑器中打开mysql_config
按下i进入插入模式。
现在查找行。
# Create options 
libs="-L$pkglibdir"
libs="$libs -l "

对我来说它在第118行。上述行应该更改为

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

现在按下 esc 按钮退出插入模式,并通过在 vi 编辑器中输入命令:wq保存添加的内容。
现在尝试使用 pip 重新安装 MySQL-Python 模块。
请注意,假设已安装 mysqlopenssl

这个解决方案是唯一一个对我有效的。谢谢! - russellkt

1
对我来说,这个有效:


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

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