Mac M1 安装 mysqlclient==2.0.3 失败解决

586 阅读2分钟

系统信息

Python 3.9.6 (default, Oct 18 2022, 12:41:40) 
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

报错日志

pip install mysqlclient==2.0.3               
Collecting mysqlclient==2.0.3
  Using cached mysqlclient-2.0.3.tar.gz (88 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py bdist_wheel did not run successfully.
  │ exit code: 1
  ╰─> [58 lines of output]
      mysql_config --version
      ['8.3.0']
      mysql_config --libs
      ['-L/usr/local/opt/mysql-client/lib', '-lmysqlclient', '-lz', '-lzstd', '-lssl', '-lcrypto', '-lresolv']
      mysql_config --cflags
      ['-I/usr/local/opt/mysql-client/include/mysql']
      ext_options:
        library_dirs: ['/usr/local/opt/mysql-client/lib']
        libraries: ['mysqlclient', 'zstd', 'resolv']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/usr/local/opt/mysql-client/include/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-12-x86_64-cpython-310
      creating build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/connections.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/converters.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/release.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      copying MySQLdb/times.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb
      creating build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.macosx-12-x86_64-cpython-310/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-12-x86_64-cpython-310
      creating build/temp.macosx-12-x86_64-cpython-310/MySQLdb
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk -Dversion_info=(2,0,3,'final',0) -D__version__=2.0.3 -I/usr/local/opt/mysql-client/include/mysql -I/Users/qiantao/work/telemetry_grpc/venv/include -I/usr/local/opt/python@3.10/Frameworks/Python.framework/Versions/3.10/include/python3.10 -c MySQLdb/_mysql.c -o build/temp.macosx-12-x86_64-cpython-310/MySQLdb/_mysql.o -std=c99
      MySQLdb/_mysql.c:521:9: error: implicit declaration of function 'mysql_ssl_set' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
              mysql_ssl_set(&(self->connection), key, cert, ca, capath, cipher);
              ^
      MySQLdb/_mysql.c:521:9: note: did you mean 'mysql_close'?
      /usr/local/opt/mysql-client/include/mysql/mysql.h:797:14: note: 'mysql_close' declared here
      void STDCALL mysql_close(MYSQL *sock);
                   ^
      MySQLdb/_mysql.c:1735:9: error: implicit declaration of function 'mysql_kill' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
          r = mysql_kill(&(self->connection), pid);
              ^
      MySQLdb/_mysql.c:1735:9: note: did you mean 'mysql_ping'?
      /usr/local/opt/mysql-client/include/mysql/mysql.h:525:13: note: 'mysql_ping' declared here
      int STDCALL mysql_ping(MYSQL *mysql);
                  ^
      MySQLdb/_mysql.c:1944:9: error: implicit declaration of function 'mysql_shutdown' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
          r = mysql_shutdown(&(self->connection), SHUTDOWN_DEFAULT);
              ^
      3 errors generated.
      error: command '/usr/bin/clang' failed with exit code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects

解决方案

从官方issue中可以看到最低修改版本是2.2.2才支持8.3版本的mysql客户端。

pip install mysqlclient==2.2.2

相关文档

github.com/PyMySQL/mys… stackoverflow.com/questions/6…