MacBook Pro下VS Code虚拟环境安装mysqlclient失败求助
问题描述
在VS Code的虚拟环境中安装mysqlclient始终失败,尝试过多种方法仍未解决。我有一个Django项目,因此无法连接MySQL数据库。
设备为2019款MacBook Pro,MySQL已安装并正常运行。
安装时的错误信息:
Collecting mysql Downloading mysql-0.0.3-py3-none-any.whl (1.2 kB) Collecting mysqlclient (from mysql) 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 ╰─> [27 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 "/builddjango/capstoneVenv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module> main() File "/builddjango/capstoneVenv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main json_out['return_val'] = hook(**hook_input['kwargs']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/builddjango/capstoneVenv/lib/python3.12/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/f2/kl4ptcws6p7_mn30tn5ktzk00000gn/T/pip-build-env-r64v109l/overlay/lib/python3.12/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/f2/kl4ptcws6p7_mn30tn5ktzk00000gn/T/pip-build-env-r64v109l/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires self.run_setup() File "/private/var/folders/f2/kl4ptcws6p7_mn30tn5ktzk00000gn/T/pip-build-env-r64v109l/overlay/lib/python3.12/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_CFLAGS="-I/usr/local/include/mysql" MYSQLCLIENT_LDFLAGS="-L/usr/local/lib/mysql -lmysqlclient" pip3 install -U mysqlclient pip install --no-cache mysqlclient==2.2.0rc1 brew install pkg-config export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"
解决方案
1. 修正路径适配Intel芯片
2019款MacBook Pro是Intel芯片,brew安装的MySQL路径为/usr/local/opt/mysql,你之前设置的PKG_CONFIG_PATH是Apple Silicon芯片的路径,这是核心问题。
先确认路径存在:
ls /usr/local/opt/mysql
激活虚拟环境后,执行以下命令:
export PKG_CONFIG_PATH="/usr/local/opt/mysql/lib/pkgconfig" brew install pkg-config mysql-client pip install mysqlclient
2. 手动指定编译参数(备选方案)
如果上述方法无效,直接指定Intel芯片的正确编译路径:
MYSQLCLIENT_CFLAGS="-I/usr/local/opt/mysql/include" MYSQLCLIENT_LDFLAGS="-L/usr/local/opt/mysql/lib -lmysqlclient" pip install mysqlclient
3. 安装编译依赖
编译mysqlclient需要Xcode命令行工具,执行以下命令安装:
xcode-select --install
安装完成后重新尝试安装mysqlclient。
4. 安装稳定版本
若仍有问题,尝试安装mysqlclient稳定版2.2.0:
pip install mysqlclient==2.2.0
内容的提问来源于stack exchange,提问作者jb1234
相关产品推荐
相关产品推荐

