如何解决Mac上Python mysqlclient安装错误

7
我正在尝试在我的MacBook上安装Python与MySQL的接口(mysqlclient 2.1.0),以便在使用Django项目(将在MS Code中开发)时使用。然而,在安装Python的mysqlclient过程中,我遇到了错误,并且无法解决它。我已经看到其他人也有类似的问题,但是通过下面的错误文本来判断,我的问题不同于他们的。
请帮忙解决。
我的系统详细信息:MAC(M1),OS Monterey 12.3,终端zsh。
操作步骤如下:
1. 下载并安装新的Python 3.10.3 (也尝试过3.9.11) 2. 创建并激活Python虚拟环境
Last login: Fri Mar 18 09:16:39 on ttys000
/Users/luklys/.zshrc:18: command not found: Password:
luklys@Luklys-MacBook-Air ~ % cd Documents/Code\ Projects/hello_django 
luklys@Luklys-MacBook-Air hello_django % python3 -m venv .myvenv
luklys@Luklys-MacBook-Air hello_django % source .myvenv/bin/activate
(.myvenv) luklys@Luklys-MacBook-Air hello_django %
  1. 尝试按照MAC上https://pypi.org/project/mysqlclient/的指示安装Python MySQL客户端。
(.myvenv) luklys@Luklys-MacBook-Air hello_django % brew install mysql-client
Warning: mysql-client 8.0.28 is already installed and up-to-date.
To reinstall 8.0.28, run:
  brew reinstall mysql-client
(.myvenv) luklys@Luklys-MacBook-Air hello_django %

(.myvenv) luklys@Luklys-MacBook-Air hello_django % echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc       
(.myvenv) luklys@Luklys-MacBook-Air hello_django % export PATH="/usr/local/opt/mysql-client/bin:$PATH"

  1. 错误信息
(.myvenv) luklys@Luklys-MacBook-Air hello_django % pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      /bin/sh: mysql_config: command not found
      /bin/sh: mariadb_config: command not found
      /bin/sh: mysql_config: command not found
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/pw/87hvnxc51gdcwf3ck25n95tw0000gn/T/pip-install-y_1qn9ke/mysqlclient_48574969a7ff4372b5ff326147c56ec6/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/private/var/folders/pw/87hvnxc51gdcwf3ck25n95tw0000gn/T/pip-install-y_1qn9ke/mysqlclient_48574969a7ff4372b5ff326147c56ec6/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/private/var/folders/pw/87hvnxc51gdcwf3ck25n95tw0000gn/T/pip-install-y_1qn9ke/mysqlclient_48574969a7ff4372b5ff326147c56ec6/setup_posix.py", line 31, in mysql_config
          raise OSError("{} not found".format(_mysql_config_path))
      OSError: mysql_config not found
      mysql_config --version
      mariadb_config --version
      mysql_config --libs
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.

6个回答

4

首先,在本地笔记本电脑或服务器上安装mysql,然后才能安装mysqlclient。

您可以从https://dev.mysql.com/doc/refman/8.0/en/macos-installation-pkg.html下载最新版本的MySQL。

接下来,将路径export PATH=${PATH}:/usr/local/mysql/bin/添加到.zshrc和.zsh_profile文件中。

添加完成后,只需运行source .zshrcsource .zsh_profile

现在尝试执行mysql -u root -p命令,如果它起作用,它会提示您输入密码或直接进入mysql。

现在您可以在Python中安装mysqlclient。


1
谢谢!我进入了我的/Users/Julius目录,添加了.zshrc.zsh_profile文件,直接复制并粘贴了"export PATH=${PATH}:/usr/local/mysql/bin/"到每个文件中,然后在两个文件上运行了$ source命令,一切都正常工作了!当然,我也双重检查了"/usr/local/mysql/bin/"文件夹是否存在。在此之后,我成功安装了mysqlclient。非常感谢!这个答案对我帮助很大 ^_^ - ideaguy3d

3

对于MariaDB,我在临时安装mariadb-connector-c后解决了问题:

brew install mariadb-connector-c
sudo ln -s /usr/local/opt/mariadb-connector-c/bin/mariadb_config /usr/local/bin/mysql_config

source ~/path-to-you-prj-enveroment/bin/activate
pip install mysqlclient
deactivate

sudo rm /usr/local/bin/mysql_config
brew unlink mariadb-connector-c

我认为使用MySQL可以以类似的方式解决问题:

brew install mysql-client
sudo ln -s /opt/homebrew/opt/mysql-client/bin/mysql_config /usr/local/bin/mysql_config

source ~/path-to-you-prj-enveroment/bin/activate
pip install mysqlclient
deactivate

rm /usr/local/bin/mysql_config
brew unlink mysql-client

2

如果有人仍然遇到这个问题(我也遇到了,使用Mac M1和Brew安装的Mariadb),这个方法有所帮助:

$ brew install mysql-client pkg-config
$ export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"
$ pip install mysqlclient

来自https://pypi.org/project/mysqlclient/

....感谢pypi提供的可行解决方案,也请原谅谷歌的各种错误...


2023年的Mac Mini配备了M1处理器。 - undefined

1

似乎MySQL未正确安装或MySQL未在路径中。为了确保,请在终端上尝试此操作:mysql -u root -p。在您的情况下,它很可能会显示此错误:zsh command not found。因此,请尝试将MySQL添加到路径中。您可以在此处查看如何将MySQL添加到Mac OS中的路径:https://devdotcode.com/how-to-add-mysql-to-the-path-in-mac-os/。如果您的MyQSL已正确安装,则应该正常工作。如果不是这种情况,请尝试重新安装MySQL。


0

我使用了python3 -m pip install mysql-client,这似乎解决了我的问题。我怀疑突然放弃python2会给很多人带来麻烦。


我尝试使用 python3 -m pip install mysql-client 而不是 brew reinstall mysql-client,安装成功了,但运行 pip install mysqlclient 仍然出现相同的错误。 - Lukasz

0

对于运行在 13.4.1 (c) (22F770820d) 上的我的 Macbook pro 16 2020 型号,Python 版本 3.11 以下步骤解决了它。

brew update
brew unlink mariadb-connector-c
brew install mariadb
brew link --overwrite mariadb
pip install mysqlclient

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