错误信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Collecting mysqlclient
Using cached mysqlclient-2.2.1.tar.gz (89 kB)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [24 lines of output]
Trying pkg-config --exists mysqlclient
Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
Trying pkg-config --exists mariadb
Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
Trying pkg-config --exists libmariadb
Command 'pkg-config --exists libmariadb' returned non-zero exit status 1.
Traceback (most recent call last):
File "/Users/wusiwei/Documents/PycharmProject/tactics/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/Users/wusiwei/Documents/PycharmProject/tactics/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/Users/wusiwei/Documents/PycharmProject/tactics/.venv/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/private/var/folders/x5/tctqgfhd405brdfffx76h2qr0000gn/T/pip-build-env-prp4xid4/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/private/var/folders/x5/tctqgfhd405brdfffx76h2qr0000gn/T/pip-build-env-prp4xid4/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
self.run_setup()
File "/private/var/folders/x5/tctqgfhd405brdfffx76h2qr0000gn/T/pip-build-env-prp4xid4/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 311, in run_setup
exec(code, locals())
File "<string>", line 155, in <module>
File "<string>", line 49, in get_config_posix
File "<string>", line 28, in find_package_name
Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

解决

根据错误日志,安装mysqlclient时遇到的问题似乎与MacOS上的依赖配置有关。
错误提示建议手动指定MYSQLCLIENT_CFLAGS和MYSQLCLIENT_LDFLAGS环境变量。以下是一些可能的解决步骤:

  1. 安装MySQL

    首先,确保你的Mac上安装了MySQL。如果尚未安装,可以通过Homebrew安装:

    1
    brew install mysql
  2. 安装pkg-config

    pkg-config是一个帮助安装软件库的工具,可以提供编译和链接时需要的选项。需要安装它:

    1
    brew install pkg-config
  3. 配置环境变量

    将下方3.1或3.2,使用的版本取决你的安装方式,这些export命令添加到您的/.bash_profile、
    /.zshrc、或其他shell配置文件中,可以使这些变量的设置在新的终端会话中保持有效。修改配置文件后,您需要重新加载配置,或者重新开启一个终端会话。重新加载配置文件的命令取决于您使用的shell,比如如果是bash,可以使用:

    1
    source ~/.bash_profile

    如果是zsh,则使用:

    1
    source ~/.zshrc
  • 3.1 (二选一)配置环境变量(brew安装版)

安装完MySQL和pkg-config后,你需要找到MySQL的头文件和库文件的位置,并设置相应的环境变量。
首先,尝试找到MySQL的安装路径。如果你使用Homebrew安装MySQL,它通常位于/usr/local/Cellar/mysql/下。路径中的版本号可能不同,所以请根据实际情况调整。

然后,设置MYSQLCLIENT_CFLAGS和MYSQLCLIENT_LDFLAGS环境变量。在终端中运行以下命令(可以直接复制使用 或 根据你的MySQL安装路径调整):

1
2
export MYSQLCLIENT_CFLAGS=$(mysql_config --cflags)
export MYSQLCLIENT_LDFLAGS=$(mysql_config --libs)
  • 3.2 (二选一)配置环境变量(手动安装版)

如果mysql是手动安装在/usr/local/mysql下的,可以直接使用这个路径来设置MYSQLCLIENT_CFLAGS和MYSQLCLIENT_LDFLAGS环境变量。重要的是找到mysql_config这个工具的确切位置,它通常位于MySQL安装目录的bin子目录下。

mysql_config应该位于/usr/local/mysql/bin/mysql_config。可以使用这个命令来生成正确的标志:

1
2
export MYSQLCLIENT_CFLAGS=$(/usr/local/mysql/bin/mysql_config --cflags)
export MYSQLCLIENT_LDFLAGS=$(/usr/local/mysql/bin/mysql_config --libs)

确保在运行这些命令之前,/usr/local/mysql/bin/mysql_config确实存在且可执行。您可以通过以下命令检查:

1
/usr/local/mysql/bin/mysql_config --version

如果这个命令返回了mysql_config的版本信息,说明它是可执行的且路径正确。
完成这些步骤后,您应该能够在Mac上成功安装mysqlclient。

提示:Mac环境变量文件操作

编辑环境变量配置文件

1
2
vim ~/.profile
vim ~/.zshrc

加载生效

1
2
source ~/.profile
source ~/.zshrc

原文链接:https://blog.csdn.net/weixin_43822632/article/details/135176777