Search code examples
pythonmysqlflaskmampflask-sqlalchemy

Can't use Flask-SqlAlchemy with mamp


I'm learning flask development & just a beginner. I'm trying to use flask-sqlalchemy ORM to connect with mysql database which is running on mamp server. However when I tried to run the application it throw this error:

ModuleNotFoundError: No module named 'MySQLdb'

After doing little research I thought I should install mysqldb. Then I tried running this command:

pip install flask-mysqldb

and I see a new error & here is the full trace:

  Collecting flask-mysqldb
    Using cached Flask-MySQLdb-0.2.0.tar.gz
  Requirement already satisfied: Flask>=0.10 in ./env/lib/python3.6/site-packages (from flask-mysqldb)
  Collecting mysqlclient (from flask-mysqldb)
    Using cached mysqlclient-1.3.10.tar.gz
      Complete output from command python setup.py egg_info:
      /bin/sh: mysql_config: command not found
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
        File "/private/var/folders/qy/81pz3q7n2l310x2r6nrjp9nr0000gn/T/pip-build-rdep43c8/mysqlclient/setup.py", line 17, in <module>
          metadata, options = get_config()
        File "/private/var/folders/qy/81pz3q7n2l310x2r6nrjp9nr0000gn/T/pip-build-rdep43c8/mysqlclient/setup_posix.py", line 44, in get_config
          libs = mysql_config("libs_r")
        File "/private/var/folders/qy/81pz3q7n2l310x2r6nrjp9nr0000gn/T/pip-build-rdep43c8/mysqlclient/setup_posix.py", line 26, in mysql_config
          raise EnvironmentError("%s not found" % (mysql_config.path,))
      OSError: mysql_config not found

I read some article regarding this issue and most of them guide to change the config on site.cfg. But unfortunately I couldn't find any file named site.cfg. I read many article regarding this issue and tried to follow their guide but nothing worked for me. Don't know but may be I'm doing some silly mistake or something. Someone please help me to get rid of this...

Thanks, Robin


Solution

  • I found the solution after doing some research here are the steps:

    1. Install the latest version of XCode & XCode developer tools.
    2. Install 'brew' package manager
    3. install MySQL using this command: brew install mysql
    4. Run this command to make a async link between MySQL socket: sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock (your socket url could be different if you using are xampp or any other local server.
    5. Start MAMP/XAMMP MySQL server
    6. Install pip install flask-mysqldb then sqlalchemy etc.

    I think all above steps explain everything & After that sqlAlchemy should connect with your database without any problem.