自学内容网 自学内容网

Mac安装pip3 install mysqlclient

问题一

背景

pip3 install mysqlclient

# 下面是报错信息
Defaulting to user installation because normal site-packages is not writeable
Collecting mysqlclient
  Using cached mysqlclient-2.2.4.tar.gz (90 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.

解决方法

直接使用 mysql_config 给出的编译标志:

export MYSQLCLIENT_CFLAGS="$(mysql_config --cflags)"
export MYSQLCLIENT_LDFLAGS="$(mysql_config --libs)"

问题二

背景

完整报错如下:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/xxx/Library/Python/3.9/lib/python/site-packages/MySQLdb/__init__.py", line 17, in <module>
    from . import _mysql
ImportError: dlopen(/Users/xxx/Library/Python/3.9/lib/python/site-packages/MySQLdb/_mysql.cpython-39-darwin.so, 0x0002): Library not loaded: @rpath/libmysqlclient.24.dylib
  Referenced from: <7C75C569-179F-382A-B665-D4B7BA60C32B> /Users/xxx/Library/Python/3.9/lib/python/site-packages/MySQLdb/_mysql.cpython-39-darwin.so
  Reason: no LC_RPATH's found

总结下来有下面三个错误:

Library not loaded: @rpath/libmysqlclient.24.dylib
Library not loaded: libssl.3.dylib
Library not loaded: libcrypto.3.dylib

解决方法

我全部建立了软链接,但还是不行,软链接如下:

    0 lrwxr-xr-x  1 root  wheel        38 Jul 31 14:44 libcrypto.3.dylib -> /usr/local/mysql/lib/libcrypto.3.dylib
    0 lrwxr-xr-x  1 root  wheel        44 Jul 31 14:40 libmysqlclient.24.dylib -> /usr/local/mysql/lib/libmysqlclient.24.dylib
    0 lrwxr-xr-x  1 root  wheel        35 Jul 31 14:43 libssl.3.dylib -> /usr/local/mysql/lib/libssl.3.dylib

使用 install_name_tool 修正路径最终可以了:

install_name_tool -add_rpath /usr/local/mysql/lib /Users/xxx/Library/Python/3.9/lib/python/site-packages/MySQLdb/_mysql.cpython-39-darwin.so
install_name_tool -change libssl.3.dylib /usr/local/lib/libssl.3.dylib /Users/xxx/Library/Python/3.9/lib/python/site-packages/MySQLdb/_mysql.cpython-39-darwin.so
install_name_tool -change libcrypto.3.dylib /usr/local/lib/libcrypto.3.dylib /Users/xxx/Library/Python/3.9/lib/python/site-packages/MySQLdb/_mysql.cpython-39-darwin.so

原文地址:https://blog.csdn.net/qq_44324181/article/details/140822185

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!