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

发布于 2025-01-15 05:07:47 字数 3095 浏览 5 评论 0原文

我正在尝试在带有 M1 芯片的 MacBook 上安装 MySQL 的 Python 接口(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 venv
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 指令安装 python MySQL 客户端 https://pypi.org/project/mysqlclient/
(.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.

I’m trying to install Python interface to MySQL (mysqlclient 2.1.0) for use with Django project (to be developed in MS Code) on my MacBook with M1 chip. When I’m trying to install mysqlclient for python I get an error and I’m unable to resolve it. I have seen other people having similar kind of issues, but mine is not the same judging by error text I get from below

Please help

My system details: MAC (M1), OS Monterey 12.3, terminal zsh

  1. Download and install new python 3.10.3 (but also tried with 3.9.11)
  2. Create and activate python venv
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. Try to install python MySQL client using MAC instructions from
    https://pypi.org/project/mysqlclient/
(.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. Error message
(.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

帅哥哥的热头脑 2025-01-22 05:07:47

您必须首先在本地笔记本电脑/服务器上安装 mysql 才能安装 mysqlclient。

您可以从 https:// /dev.mysql.com/doc/refman/8.0/en/macos-installation-pkg.html

然后添加路径 export PATH=${PATH}:/usr/local/mysql/bin/
添加后到 .zshrc 和 .zsh_profile

只需运行 source .zshrcsource .zsh_profile

现在尝试执行 mysql -u root -p 如果它它会提示输入密码或直接带你到mysql。

现在你可以在python中安装mysqlclient了。

You have to install mysql on your local laptop/server first to install mysqlclient.

You can download the latest version of MySQL from https://dev.mysql.com/doc/refman/8.0/en/macos-installation-pkg.html

and then add path export PATH=${PATH}:/usr/local/mysql/bin/
to .zshrc and .zsh_profile

after adding just run source .zshrc and source .zsh_profile

Now try to do mysql -u root -p if it works it will prompt to enter password or directly take you to mysql.

Now you can install mysqlclient in python.

遥远的绿洲 2025-01-22 05:07:47

对于 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

For MariaDB I solve problem after temporary install 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

I believe that with MySQL the problem is solved in a similar way:

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
书间行客 2025-01-22 05:07:47

以防万一有人仍然有问题(我做了。mac m1,mariadb 和brew)。这有帮助:

$ 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 的工作解决方案,并原谅谷歌的各种错误......

just in case someone still has the issue (I did. mac m1, mariadb with brew). This helped:

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

from https://pypi.org/project/mysqlclient/

....and thank you oh pypi for working solution, and forgive google for all sorts of wrong...

香草可樂 2025-01-22 05:07:47

看来 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。

It seems either MySQL is not installed properly or MySQL is NOT in the path. To be sure, try this on your terminal: mysql -u root -p. In your case, it will most probably give this error: zsh command not found. So, try to add MySQL to your path. You can look at how to add MySQL to the path in Mac OS here:
https://devdotcode.com/how-to-add-mysql-to-the-path-in-mac-os/ . It should work if your MyQSL is installed properly. If this is not the case, try to reinstall MySQL properly.

女中豪杰 2025-01-22 05:07:47

我使用了 python3 -m pip install mysql-client ,这似乎解决了我的问题。我怀疑 python2 的意外抛弃会让很多人头疼。

I used python3 -m pip install mysql-client and that seemed to resolve my issue. The surprise dumping of python2 i suspect will cause headaches for many folks.

萤火眠眠 2025-01-22 05:07:47

对于我在 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

For my Macbook pro 16 2020 model running on 13.4.1 (c) (22F770820d), python version 3.11 below steps solved it.

brew update
brew unlink mariadb-connector-c
brew install mariadb
brew link --overwrite mariadb
pip install mysqlclient
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文