macOS安装mysqlclient报下面的错:

省略1万字。。。
clang -bundle -undefined dynamic_lookup -L/opt/homebrew/opt/ncurses/lib -L/Users/XX/.pyenv/versions/3.7.17/readline/lib -L/Users/XX/.pyenv/versions/3.7.17/lib -Wl,-rpath,/Users/XX/.pyenv/versions/3.7.17/lib -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -L/opt/homebrew/opt/ncurses/lib -L/Users/XX/.pyenv/versions/3.7.17/readline/lib -L/Users/XX/.pyenv/versions/3.7.17/lib -Wl,-rpath,/Users/XX/.pyenv/versions/3.7.17/lib -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib build/temp.macosx-15.3-arm64-cpython-37/_mysql.o -L/usr/local/mysql/lib -L/Users/XX/.pyenv/versions/3.7.17/lib -lmysqlclient -limported_openssl -limported_crypto -o build/lib.macosx-15.3-arm64-cpython-37/_mysql.cpython-37m-darwin.so
ld: warning: duplicate -rpath '/Users/XX/.pyenv/versions/3.7.17/lib' ignored
ld: warning: duplicate -rpath '/opt/homebrew/lib' ignored
ld: library 'imported_openssl' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit code 1

意思就是imported_openssl没找到,可以试试下面的办法:
1、直接下载安装包 wget https://files.pythonhosted.org/packages/74/ff/4e964e20b559e55d7afa60fbccc6a560f2adf289813bd3d7eb4eb8a87093/mysqlclient-1.3.7.tar.gz

2、解压 tar -zxf mysqlclient-1.3.7.tar.gz
3、进入目录 cd mysqlclient-1.3.7
4、手动安装 python setup.py install,正常来讲会报上面的错,找到报错上面clang的那句指令把里面的"-limported_openssl -limported_crypto"去掉得到下面的命令(根据自己的环境有所不同),手动执行这条指令,正常的话能编译过。

clang -bundle -undefined dynamic_lookup -L/opt/homebrew/opt/ncurses/lib -L/Users/XX/.pyenv/versions/3.7.17/readline/lib -L/Users/XX/.pyenv/versions/3.7.17/lib -Wl,-rpath,/Users/XX/.pyenv/versions/3.7.17/lib -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -L/opt/homebrew/opt/ncurses/lib -L/Users/XX/.pyenv/versions/3.7.17/readline/lib -L/Users/XX/.pyenv/versions/3.7.17/lib -Wl,-rpath,/Users/XX/.pyenv/versions/3.7.17/lib -L/opt/homebrew/lib -Wl,-rpath,/opt/homebrew/lib -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib build/temp.macosx-15.3-arm64-cpython-37/_mysql.o -L/usr/local/mysql/lib -L/Users/XX/.pyenv/versions/3.7.17/lib -lmysqlclient -o build/lib.macosx-15.3-arm64-cpython-37/_mysql.cpython-37m-darwin.so

5、再次执行python setup.py install,因为需要的so文件刚刚已经构建完了,所以不会失败了,可以用命令python -c "import MySQLdb;print(MySQLdb.__file__)试试正常没。如果报“'libmysqlclient.18.dylib' (no such file)”错的话可以看这篇 https://segmentfault.com/a/1190000045691431


RobinTang
3.9k 声望37 粉丝