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/
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.