「macOS環境でmysqlclientのインストールがうまくいかない」とチーム内で相談があり、自分の環境でもうまくいかなかったので解決までのメモを残しておきます。
環境は、Mac mini 2023 Apple M2, macOS Sonoma 14.6, Python3.12です。
MySQLクライアントとpkg-configはHomebrewでインストールしました。
brew install mysqlclient pkg-config
この状態で、venv環境でpip installでrequirements.txtからmysqlclientをインストールしたところ、pkg-configでmysqlclientのライブラリを見つけられない旨のエラーとなりました。
Collecting mysqlclient==2.2.6 (from -r requirements.txt (line 3))
Using cached mysqlclient-2.2.6.tar.gz (91 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
╰─> [29 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.
Trying pkg-config --exists perconaserverclient
Command 'pkg-config --exists perconaserverclient' returned non-zero exit status 1.
Traceback (most recent call last):
File "/Users/okano/work/myproject/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/Users/okano/work/myproject/venv/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 "/Users/okano/work/myproject/venv/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/23/9rdcmsds2d7ff4937xnd7t4h0000gn/T/pip-build-env-xtvhobpp/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 334, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=[])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/private/var/folders/23/9rdcmsds2d7ff4937xnd7t4h0000gn/T/pip-build-env-xtvhobpp/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 304, in _get_build_requires
self.run_setup()
File "/private/var/folders/23/9rdcmsds2d7ff4937xnd7t4h0000gn/T/pip-build-env-xtvhobpp/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 320, 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.
StackOverflowに同様の事象の質問があり、回答にある PKG_CONFIG_PATH を環境変数に設定したら解決しました。
venvが有効な状態で、シェルからexportコマンドにて環境変数を設定
export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"
この状態で再度インストールを実施したところ、成功しました。