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/
Shubham Pandey
  • 111
  • 1
  • 1
  • 9
  • you have to install database MySQL – furas Jul 21 '19 at 06: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. – Shubham Pandey Jul 21 '19 at 08: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` – furas Jul 21 '19 at 08: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` – furas Jul 21 '19 at 09:00
  • yes..i also tried `sudo apt install libmysqlclient18 libmysqlclient-dev` but i am getting not available error. – Shubham Pandey Jul 21 '19 at 09:02
  • in the meanwhile i also found a similar question which describes nearly the same problem as me [link](https://stackoverflow.com/questions/34030215/error-loading-mysqldb-module-and-pip-install-mysqldb) but it get solved after installing `libmysqlclient-dev` followed by installing `MySQL-python`...in my case i am getting another error while installing `MySQL-python` which i am updating in the question – Shubham Pandey Jul 21 '19 at 09:08
  • maybe use `mysql-connector-python` instead of `flask-mysql`. Module `mysql-connector-python` is created by authors of MySQL and it is always fresh. – furas Jul 21 '19 at 09:11

2 Answers2

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.

None
  • 572
  • 1
  • 5
  • 12
  • Thanks for answering but the problem is solved by following this thread [link](https://askubuntu.com/questions/1159764/not-able-to-install-package-flask-mysqldb) – Shubham Pandey Jul 24 '19 at 06:54
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
Umesh Chaudhary
  • 391
  • 1
  • 6