3

I was trying to learn to link flask with MySQL database from this tutorial.

I installed flask-mysqldb using pip3 into my virtual environment which was successful.

There are some answers but they all cover fail to install flask-mysqldb in my case the installation itself is successful but I am still getting the error on the webpage.

When I am starting the server using flask run and trying to run the routed URL localhost:5000/ into the browser I am getting an Import Error for flask-mysqldb.

This is the exact error I am getting:

flask.cli.NoAppException: While importing "todo_app", an ImportError was raised:

File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)

File "/home/shubham/repos/academy-hackathon/week-0/day-3/my_todo_app/todo_app/app.py", line 7, in <module> 
from flask_mysqldb import MySQL 

File "/home/shubham/venv/lib/python3.6/site-packages/flask_mysqldb/__init__.py", line 1, in <module> 
import MySQLdb 

File "/home/shubham/venv/lib/python3.6/site-packages/MySQLdb/__init__.py", line 18, in <module> 
from . import _mysql 

ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 338, in __call__
    self._flush_bg_loading_exception()
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 326, in _flush_bg_loading_exception
    reraise(*exc_info)
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 314, in _load_app
    self._load_unlocked()
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 330, in _load_unlocked
    self._app = rv = self.loader()
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 388, in load_app
    app = locate_app(self, import_name, name)
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 247, in locate_app
    "\n\n{tb}".format(name=module_name, tb=traceback.format_exc())
flask.cli.NoAppException: While importing "app", an ImportError was raised:

Traceback (most recent call last):
  File "/home/shubham/venv/lib/python3.6/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/shubham/repos/academy-hackathon/week-0/day-3/my_todo_app/todo_app/app.py", line 7, in <module>
    from flask_mysqldb import MySQL
  File "/home/shubham/venv/lib/python3.6/site-packages/flask_mysqldb/__init__.py", line 1, in <module>
    import MySQLdb
  File "/home/shubham/venv/lib/python3.6/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory

venv is the name of the python environment

I am not sure why I am getting this errror, I am running the server from the same python environment in which I installed the module flask-mysqldb.

Also, I have anaconda-navigator installed on my machine on 'base' named environment I tried installing the module using conda install flask-mysqldb but it shows no module error. So is it possible that the error is due to the conda environment?

Please suggest any solution, stuck on this from last night.

Edit: I found here that installing MySQL-python should fixed this problem, but I am getting another error while installing MySQL-python

Collecting MySQL-python
  Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
    ERROR: Complete output from command python setup.py egg_info:
    ERROR: Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-9zyrtdz5/MySQL-python/setup.py", line 13, in <module>
        from setup_posix import get_config
      File "/tmp/pip-install-9zyrtdz5/MySQL-python/setup_posix.py", line 2, in <module>
        from ConfigParser import SafeConfigParser
    ModuleNotFoundError: No module named 'ConfigParser'
    ----------------------------------------
ERROR: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-9zyrtdz5/MySQL-python/
7
  • you have to install database MySQL Commented Jul 21, 2019 at 6:35
  • @furas did you mean MySQL server...in that case I have already installed it..but in case you mean MySQL database...can you share some link for the installation of the same...as searching it is only giving methods to install MySQL server. Commented Jul 21, 2019 at 8:30
  • on Linux Mint and I have installed mysql-server libmysqlclient20 using apt-get. but I don't use flask-mysql but rather SQLAlchemy, peewee or mysql.connector Commented Jul 21, 2019 at 8:59
  • in error message I see libmysqlclient.so.18 which may means it needs old version libmysqlclient18 which you may not have in apt-get Commented Jul 21, 2019 at 9:00
  • yes..i also tried sudo apt install libmysqlclient18 libmysqlclient-dev but i am getting not available error. Commented Jul 21, 2019 at 9:02

2 Answers 2

4

First, install Flask-MySQLdb:

$ pip install flask-mysqldb

Flask-MySQLdb depends, and will install for you, recent versions of Flask (0.12.4 or later) and mysqlclient. Flask-MySQLdb is compatible with and tested on Python 2.7, 3.5, 3.6 and 3.7.

Next, add a MySQL instance to your code:

from flask import Flask
from flask_mysqldb import MySQL

..from the info provided - here.

link to the documentation - here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for answering but the problem is solved by following this thread link
0

Try this in the same order

sudo apt install python3.6-dev libpython3.6-dev
sudo apt-get install mysql-server
virtualenv -p python3.6 venv
source venv/bin/activate
pip3 install flask
pip3 install flask-mysqldb

then create app.py

from flask import Flask

app = Flask(__name__)


if __name__ == '__main__':
    app.run()

then execute the app module

python app.py

9 Comments

still getting the same error...even after installing the above package
then i guess its setup issue. Can you please mention the python version, OS platform your are using?
ubuntu 18.04...and python 3.6.8
Updated the answer for more detailed setup
any ways to reset it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.