PyMySQL/mysqlclient

failed to install: pip install mysqlclient (MySQL 8.0.33)

giuseppenovielli opened this issue · 16 comments

Describe the bug

When try into virtualenv but also in global environments -> pip install mysqlclient
failed to install with the follow stacktrace:

Collecting mysqlclient
  Using cached mysqlclient-2.1.1.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
  ╰─> [43 lines of output]
      mysql_config --version
      ['8.0.33']
      mysql_config --libs
      ['-L/opt/homebrew/opt/mysql-client/lib', '-lmysqlclient', '-lzlib', '-lzstd', '-L/opt/homebrew/opt/openssl@1.1/lib', '-lssl', '-lcrypto', '-lresolv']
      mysql_config --cflags
      ['-I/opt/homebrew/opt/mysql-client/include/mysql']
      ext_options:
        library_dirs: ['/opt/homebrew/opt/mysql-client/lib', '/opt/homebrew/opt/openssl@1.1/lib']
        libraries: ['mysqlclient', 'zlib', 'resolv']
        extra_compile_args: ['-std=c99']
        extra_link_args: []
        include_dirs: ['/opt/homebrew/opt/mysql-client/include/mysql']
        extra_objects: []
        define_macros: [('version_info', "(2,1,1,'final',0)"), ('__version__', '2.1.1')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-10.9-universal2-3.9
      creating build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/__init__.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/_exceptions.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/connections.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/converters.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/cursors.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/release.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      copying MySQLdb/times.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb
      creating build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      copying MySQLdb/constants/CR.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      copying MySQLdb/constants/ER.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.9-universal2-3.9/MySQLdb/constants
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-10.9-universal2-3.9
      creating build/temp.macosx-10.9-universal2-3.9/MySQLdb
      clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -Dversion_info=(2,1,1,'final',0) -D__version__=2.1.1 -I/opt/homebrew/opt/mysql-client/include/mysql -I/Users/hrcoffee6/my_venv/django_3/include -I/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-10.9-universal2-3.9/MySQLdb/_mysql.o -std=c99
      clang -bundle -undefined dynamic_lookup -arch arm64 -arch x86_64 -Wl,-headerpad,0x1000 build/temp.macosx-10.9-universal2-3.9/MySQLdb/_mysql.o -L/opt/homebrew/opt/mysql-client/lib -L/opt/homebrew/opt/openssl@1.1/lib -lmysqlclient -lzlib -lresolv -o build/lib.macosx-10.9-universal2-3.9/MySQLdb/_mysql.cpython-39-darwin.so
      ld: library not found for -lzlib
      clang: error: linker command failed with exit code 1 (use -v to see invocation)
      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

Environment

Macbook Pro M2
Python 3.9.6

MySQL Server

  • 8.0.33 (Homebrew)

MySQL Client

  • OS (MacOS Ventura 13.3.1 (a)):

Additional context

brew install mysqlclient -> installed successfully

https://stackoverflow.com/questions/76177559/library-not-found-for-lzlib-mac-m2-chip-pip-install-mysqlclient

it seems mysql 8.0.33 issue -> Homebrew/homebrew-core#130258

WORKAROUND -> if you use Django:

Use Oracle MySQL Connector/Python
(https://docs.djangoproject.com/en/4.2/ref/databases/#mysql-db-api-drivers

1)Download https://dev.mysql.com/downloads/connector/python/
2)DATABASES = { 'default': { 'ENGINE': 'mysql.connector.django', 'NAME': '<name_db>', 'USER': '<user_db>', 'PASSWORD': '<pass_db>', 'HOST': 'localhost', 'PORT': '3306', } }

It seems MySQL bug. They break mysql_config often.

I've tried almost all available solutions on google and failed.
M2 Ventura is eating my 3hours..

$ export MYSQLCLIENT_LDFLAGS=$(pkg-config --libs mysqlclient)
$ export MYSQLCLIENT_CFLAGS=$(pkg-config --cflags mysqlclient)
$ pip install mysqlclient

@methane , Adding those variables worked for me. Thanks!

@methane Thanks a lot! It works for me.

Thanks @methane, it works for me also!

@methane
got message "zsh: command not found: pkg-config"
what could be the problem?

Do Google. I don't know your environment.

@2twenity

mac os global environment run: ->
brew install pkg-config

in your virtualenv or other environment ->

export MYSQLCLIENT_LDFLAGS=$(pkg-config --libs mysqlclient)
export MYSQLCLIENT_CFLAGS=$(pkg-config --cflags mysqlclient)
pip install mysqlclient

@giuseppenovielli This worked! Thank you

This worked, thank you so much!!

I have released 2.2.0rc1 that uses pkg-config instead of broken mysql_config.
Please try it.
https://pypi.org/project/mysqlclient/2.2.0rc1/

2.2.0rc1 is fixed, following @methane previews post:

# ensure pkg-config is installed
brew install pkg-config

# Follow https://pypi.org/project/mysqlclient/2.2.0rc1/ "macOS (Homebrew)" section instructions
export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"

# 2.2.0rc1 installation works
pip install --no-cache mysqlclient==2.2.0rc1

To have a complete solution in linux... try this

apt install pkg-config
export MYSQLCLIENT_LDFLAGS=$(pkg-config --libs mysqlclient)
export MYSQLCLIENT_CFLAGS=$(pkg-config --cflags mysqlclient)
pip install mysqlclient --no-cache-dir

or as stated in the official page

apt install -y pkg-config                                  
export MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags`
export MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs` 
pip3 install mysqlclient --no-cache-dir                    

No need to set MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS if pkg-config works.