How to resolve Python mysqlclient install error on Mac
Asked Answered
U

6

7

I’m trying to install Python interface to MySQL (mysqlclient 2.1.0) for use with Django project (to be developed in MS Code) on my MacBook with M1 chip. When I’m trying to install mysqlclient for python I get an error and I’m unable to resolve it. I have seen other people having similar kind of issues, but mine is not the same judging by error text I get from below

Please help

My system details: MAC (M1), OS Monterey 12.3, terminal zsh

  1. Download and install new python 3.10.3 (but also tried with 3.9.11)
  2. Create and activate python venv
Last login: Fri Mar 18 09:16:39 on ttys000
/Users/luklys/.zshrc:18: command not found: Password:
luklys@Luklys-MacBook-Air ~ % cd Documents/Code\ Projects/hello_django 
luklys@Luklys-MacBook-Air hello_django % python3 -m venv .myvenv
luklys@Luklys-MacBook-Air hello_django % source .myvenv/bin/activate
(.myvenv) luklys@Luklys-MacBook-Air hello_django %
  1. Try to install python MySQL client using MAC instructions from https://pypi.org/project/mysqlclient/
(.myvenv) luklys@Luklys-MacBook-Air hello_django % brew install mysql-client
Warning: mysql-client 8.0.28 is already installed and up-to-date.
To reinstall 8.0.28, run:
  brew reinstall mysql-client
(.myvenv) luklys@Luklys-MacBook-Air hello_django %

(.myvenv) luklys@Luklys-MacBook-Air hello_django % echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc       
(.myvenv) luklys@Luklys-MacBook-Air hello_django % export PATH="/usr/local/opt/mysql-client/bin:$PATH"

  1. Error message
(.myvenv) luklys@Luklys-MacBook-Air hello_django % pip install mysqlclient
Collecting mysqlclient
  Using cached mysqlclient-2.1.0.tar.gz (87 kB)
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error
  
  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      /bin/sh: mysql_config: command not found
      /bin/sh: mariadb_config: command not found
      /bin/sh: mysql_config: command not found
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/private/var/folders/pw/87hvnxc51gdcwf3ck25n95tw0000gn/T/pip-install-y_1qn9ke/mysqlclient_48574969a7ff4372b5ff326147c56ec6/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/private/var/folders/pw/87hvnxc51gdcwf3ck25n95tw0000gn/T/pip-install-y_1qn9ke/mysqlclient_48574969a7ff4372b5ff326147c56ec6/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/private/var/folders/pw/87hvnxc51gdcwf3ck25n95tw0000gn/T/pip-install-y_1qn9ke/mysqlclient_48574969a7ff4372b5ff326147c56ec6/setup_posix.py", line 31, in mysql_config
          raise OSError("{} not found".format(_mysql_config_path))
      OSError: mysql_config not found
      mysql_config --version
      mariadb_config --version
      mysql_config --libs
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.

Ursulaursulette answered 18/3, 2022 at 1:28 Comment(0)
F
4

You have to install mysql on your local laptop/server first to install mysqlclient.

You can download the latest version of MySQL from https://dev.mysql.com/doc/refman/8.0/en/macos-installation-pkg.html

and then add path export PATH=${PATH}:/usr/local/mysql/bin/ to .zshrc and .zsh_profile

after adding just run source .zshrc and source .zsh_profile

Now try to do mysql -u root -p if it works it will prompt to enter password or directly take you to mysql.

Now you can install mysqlclient in python.

Fiscus answered 28/6, 2022 at 18:39 Comment(1)
Thanks I went to my /Users/Julius directory, added the .zshrc and .zsh_profile files, literally copied & pasted "export PATH=${PATH}:/usr/local/mysql/bin/" into each file, ran $ source on both of them, and everything worked! Of course I double checked that the "/usr/local/mysql/bin/" folder existed as well. After this I was able to install mysqlclient. Thanks a lot! This answer helped me a lot ^_^Scriven
V
3

For MariaDB I solve problem after temporary install mariadb-connector-c:

brew install mariadb-connector-c
sudo ln -s /usr/local/opt/mariadb-connector-c/bin/mariadb_config /usr/local/bin/mysql_config

source ~/path-to-you-prj-enveroment/bin/activate
pip install mysqlclient
deactivate

sudo rm /usr/local/bin/mysql_config
brew unlink mariadb-connector-c

I believe that with MySQL the problem is solved in a similar way:

brew install mysql-client
sudo ln -s /opt/homebrew/opt/mysql-client/bin/mysql_config /usr/local/bin/mysql_config

source ~/path-to-you-prj-enveroment/bin/activate
pip install mysqlclient
deactivate

rm /usr/local/bin/mysql_config
brew unlink mysql-client
Valdez answered 22/10, 2022 at 23:26 Comment(0)
D
3

just in case someone still has the issue (I did. mac m1, mariadb with brew). This helped:

$ brew install mysql-client pkg-config
$ export PKG_CONFIG_PATH="/opt/homebrew/opt/mysql-client/lib/pkgconfig"
$ pip install mysqlclient

from https://pypi.org/project/mysqlclient/

....and thank you oh pypi for working solution, and forgive google for all sorts of wrong...

Dingbat answered 1/9, 2023 at 17:19 Comment(1)
works mac mini 2023 M1 processorSwett
H
1

It seems either MySQL is not installed properly or MySQL is NOT in the path. To be sure, try this on your terminal: mysql -u root -p. In your case, it will most probably give this error: zsh command not found. So, try to add MySQL to your path. You can look at how to add MySQL to the path in Mac OS here: https://devdotcode.com/how-to-add-mysql-to-the-path-in-mac-os/ . It should work if your MyQSL is installed properly. If this is not the case, try to reinstall MySQL properly.

Honky answered 4/6, 2022 at 23:51 Comment(0)
K
0

I used python3 -m pip install mysql-client and that seemed to resolve my issue. The surprise dumping of python2 i suspect will cause headaches for many folks.

Kobylak answered 18/3, 2022 at 19:15 Comment(1)
I did try to use python3 -m pip install mysql-client instead of brew reinstall mysql-client which installed just fine but when running pip install mysqlclient still getting the same errorUrsulaursulette
M
0

For my Macbook pro 16 2020 model running on 13.4.1 (c) (22F770820d), python version 3.11 below steps solved it.

brew update
brew unlink mariadb-connector-c
brew install mariadb
brew link --overwrite mariadb
pip install mysqlclient
Microgroove answered 9/8, 2023 at 21:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.