Installing flask-mysqldb (Debug and Solution)

with 4 Comments

In this post, I will go through how I was able to install flask-mysqldb after going through lots of trial and error. I wanted to install flask-mysqldb to connect the Flask API to the MySQL database we were planning on using.

To install flask-mysqldb, the command to run is:

pip install flask_mysqldb

Yet, when I ran it, I kept receiving these errors:

jessicapeng@Jessicas-MacBook-Pro-2 ~ % pip install flask_mysqldb
Collecting flask_mysqldb
  Using cached Flask-MySQLdb-0.2.0.tar.gz (2.1 kB)
Collecting Flask>=0.10
  Using cached Flask-1.1.2-py2.py3-none-any.whl (94 kB)
Collecting mysqlclient
  Using cached mysqlclient-1.4.6.tar.gz (85 kB)
    ERROR: Command errored out with exit status 1:
     command: /System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/__/j8b0d7wn7_796hd4c4x56f040000gn/T/pip-install-rCiijx/mysqlclient/setup.py'"'"'; __file__='"'"'/private/var/folders/__/j8b0d7wn7_796hd4c4x56f040000gn/T/pip-install-rCiijx/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /private/var/folders/__/j8b0d7wn7_796hd4c4x56f040000gn/T/pip-pip-egg-info-0D9Xsz
         cwd: /private/var/folders/__/j8b0d7wn7_796hd4c4x56f040000gn/T/pip-install-rCiijx/mysqlclient/
    Complete output (12 lines):
    sh: mysql_config: command not found
    sh: mariadb_config: command not found
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/__/j8b0d7wn7_796hd4c4x56f040000gn/T/pip-install-rCiijx/mysqlclient/setup.py", line 16, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 61, in get_config
        libs = mysql_config("libs")
      File "setup_posix.py", line 29, in mysql_config
        raise EnvironmentError("%s not found" % (_mysql_config_path,))
    EnvironmentError: mysql_config not found
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

In the error, it indicated that  mysql_config: command not found so I found this thread referring to it in my search: https://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface

In the thread, it mentions to use  apt-get but I had trouble running this command in terminal, and I learned that  apt-get is only for debian and ubuntu, whereas yum is for centos/fedora/redhat distribution. I decided to use brew which is a 3rd party community doing unix-like things on macOS.

When I attempted to install with brew using this command:

brew install mysql-client

I received these errors:

mysql-client is keg-only, which means it was not symlinked into /usr/local,
because it conflicts with mysql (which contains client libraries).If you need to have mysql-client first in your PATH run:
  echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrcFor compilers to find mysql-client you may need to set:
  export LDFLAGS="-L/usr/local/opt/mysql-client/lib"
  export CPPFLAGS="-I/usr/local/opt/mysql-client/include"

Which essentially tells me to put it into my .zshrc folder. That is done through this command:

echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc

This briefly translates to:

“Please update my STARTUP config, and when I start a new terminal, in that terminal, update my PATH”

Note: Just editing .zhrc does not affect current window

Afterwards, a new terminal must be opened (my previous mistake with checking in the same terminal window and being confused that no path had been added) in order to check the path with:

echo $PATH

I was able to see the path added after:

(base) jessicapeng@Jessicas-MacBook-Pro ~ % echo $PATH
/usr/local/opt/mysql-client/bin:/Users/jessicapeng/opt/anaconda3/bin:/Users/jessicapeng/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin

Afterwards, when I tried to install flask-mysqldb with the command:

pip install flask-mysqldb

It worked and installed flask-mysqldb successfully! Afterwards, I was able to run the flask program I had built!

This took me hours of stack overflow,  so hopefully it’s helpful for those who have run into the same problem with installing flask-mysqldb!

 

4 Responses

  1. Rob
    | Reply

    Thanks! Just ran into same problem and this saved me some time!

  2. Anirban
    | Reply

    Great, almost worked.
    Then brew reinstall mysql fixed all my problems after i removed mysqlclient==2.0.1 from my requirements.txt

    https://superuser.com/questions/391701/how-do-i-fix-library-not-loaded-libssl-1-0-0-dylib-with-postgresql

  3. anonymous
    | Reply

    Thank you so much! Worked for me too.

  4. Programmer
    | Reply

    Worked

Leave a Reply