我有一台新的 Macbook Pro M3 Pro,我正在尝试在其上安装 Django 项目。
尝试安装 mysqlclient python 包时,
pip install -r requirements.txt
命令返回错误。错误如下:
Building wheel for mysqlclient (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [44 lines of output]
# Options for building extension module:
extra_compile_args: ['-I/opt/homebrew/opt/mysql-client/include', '-std=c99']
extra_link_args: ['-L/opt/hombrew/opt/mysql-client/lib']
define_macros: [('version_info', (2, 2, 4, 'final', 0)), ('__version__', '2.2.4')]
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-11.1-arm64-cpython-312
creating build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/release.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/cursors.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/connections.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/__init__.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/times.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/converters.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
copying src/MySQLdb/_exceptions.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
creating build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
copying src/MySQLdb/constants/FLAG.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
copying src/MySQLdb/constants/CLIENT.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
copying src/MySQLdb/constants/__init__.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
copying src/MySQLdb/constants/ER.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
copying src/MySQLdb/constants/CR.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
copying src/MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb/constants
running egg_info
writing src/mysqlclient.egg-info/PKG-INFO
writing dependency_links to src/mysqlclient.egg-info/dependency_links.txt
writing top-level names to src/mysqlclient.egg-info/top_level.txt
reading manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
adding license file 'LICENSE'
writing manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
copying src/MySQLdb/_mysql.c -> build/lib.macosx-11.1-arm64-cpython-312/MySQLdb
running build_ext
building 'MySQLdb._mysql' extension
creating build/temp.macosx-11.1-arm64-cpython-312
creating build/temp.macosx-11.1-arm64-cpython-312/src
creating build/temp.macosx-11.1-arm64-cpython-312/src/MySQLdb
clang -fno-strict-overflow -DNDEBUG -O2 -Wall -fPIC -O2 -isystem /opt/homebrew/anaconda3/envs/camimpeg/include -arch arm64 -fPIC -O2 -isystem /opt/homebrew/anaconda3/envs/camimpeg/include -arch arm64 -I/opt/homebrew/include "-Dversion_info=(2, 2, 4, 'final', 0)" -D__version__=2.2.4 -I/opt/homebrew/anaconda3/envs/camimpeg/include/python3.12 -c src/MySQLdb/_mysql.c -o build/temp.macosx-11.1-arm64-cpython-312/src/MySQLdb/_mysql.o -I/opt/homebrew/opt/mysql-client/include -std=c99
src/MySQLdb/_mysql.c:29:10: fatal error: 'mysql.h' file not found
#include "mysql.h"
^~~~~~~~~
1 error generated.
error: command '/usr/bin/clang' failed with exit code 1
在要求中,包的版本是
mysqlclient==2.1.1
并且运行命令 pip install mysqlclient
会给出相同的错误。
我已经尝试过
brew install mysql-client pkg-config
命令,我还向 ~/.zshrc
文件添加了变量:
MYSQLCLIENT_LDFLAGS="-L/opt/hombrew/opt/mysql-client/lib"
MYSQLCLIENT_CFLAGS="-I/opt/homebrew/opt/mysql-client/include"
PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"
如这个问题所示:Cannot install mysqlclient on MacOS with no good result.
我还使用命令
brew install mysql
安装了mysql,结果相同。
我也使用通过brew安装的anaconda3,在项目中我将使用MAMP进行mysql数据库进程管理。
我找到了解决这个问题的方法。我以另一种方式定义了环境变量,以便可以正确构建包。变量是:
export MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags`
export MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs`
您需要安装 pkg-config。可以使用
brew install pkg-config
安装。
定义变量后,只需运行
pip install mysqlclient
,它应该可以正常工作。