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.
Answer from None on Stack OverflowFirst, 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.
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
You'll have to install it first. It's not packaged in Ubuntu repositories, so you'll have to use pip2.
pip2 install flask-mysqldb
or if you want to install it systemwide
sudo pip2 install flask-mysqldb
Further documentation is provided by the project itself
for linux you have to install library for python mysql client if you are in linux run a sudo
sudo apt-get install python3.6-dev libmysqlclient-dev
note you have to modify your paython version in above command
No module named flaskext.mysql
Need help with mysql error.
I see the virtual environment set up in your path. Was this working before outside the virtual environment? Did you move to venv and not have the libraries there? Or, did you switch venvs and it was loaded in the other one?
Just throwing things out there...
More on reddit.comflask-mysqldb problem : Forums : PythonAnywhere
No module named flaskext : Forums : PythonAnywhere
Hi, I have been working on a flask app for a little over a month with no problem, but today I could not run the app due to a mysql error. Specifically the error was:
File "app.py", line 4, in <module>
from flask_mysqldb import MySQL
File "C:\Users\gztau\documents\Atom_Flask\venv\lib\site-packages\flask_mysqldb\__init__.py", line 1, in <module>
import MySQLdb
File "C:\Users\gztau\documents\Atom_Flask\venv\lib\site-packages\MySQLdb\__init__.py", line 19, in <module>
import _mysql
ModuleNotFoundError: No module named '_mysql'
Mysql has been working fine up til now. I have never changed anything in any of the venv, or init files, this is the first time ive even looked at any of it. Any suggestions on what is going on, i am at a loss. Im pretty new to programming, getting better with flask etc, and was just using it last night. Sorry to bother, but thanks for any help.
I see the virtual environment set up in your path. Was this working before outside the virtual environment? Did you move to venv and not have the libraries there? Or, did you switch venvs and it was loaded in the other one?
Just throwing things out there...
mysql don't have official library for python3 anymore. install pymysql, and use this in app.py. pymysql.install_as_MySQLdb() and use flask_sqlalchemy is a good orm.
» pip install Flask-MySQLdb
First time using mysql…ImportError:
I have looked all over and tried everything to resolve this issue for a day or two, but haven't had any luck.
I am using flask and sql for the first time(macOS), I have everything installed. But I am guessing that it is either installed incorrectly, or in the wrong folder.
I installed sql directly from their website, I then followed this advice here...
$ open -t .bash_profile export PATH=${PATH}:/usr/local/mysql/bin/I know that it does work because I am able to go into the database in my terminal. my Flask app does work (it runs before adding all of the sql code), but the only thing I am having trouble with is running my program after importing(from flask_mysqldb import MySQL).
This is the tutorial I am following as well, https://www.youtube.com/watch?v=6L3HNyXEais
my error is here...
Error: While importing "app", an ImportError was raised:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask/cli.py", line 235, in locate_app __import__(module_name) File "/Users/jonathanmast/flask_prac/app.py", line 2, in <module> from flask_mysqldb import MySQL File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_mysqldb/__init__.py", line 1, in <module> import MySQLdb File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module> from . import _mysql ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.18.dylib Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
This is definitely a Python problem. You'd have more luck on learnpython subreddit. It's hard to read the stack trace on my phone but it's probably a path issue. Either where you're impirting your flask app or something changed with flask between when the video was recorded and 3.7 Python or whatever the most recent version of Flask is.
I second what u/mkingsbu said that this is more of a python issue than a sql/mysql issue and you may find better assistance on a python board.
That error however is due to a miss-linked or missing core package that the python module is looking for. When you import a package from a script the module must be installed (e.g. pip install mysql) and the requisite dependencies must also be installed. What you’re likely missing is the OS package (e.g. apt-get python-mysql, or whatever the OSX variant is, I’m not a Max person).
ImportError is a bit vague as it will be thrown if the code point cannot be resolved in some way. The stacktrace will indicate where in the dependency chain the load failed. In your error, the .so cannot be found which indicates a operating system level package cannot be found. To identify which package, you may want to seek assistance from the package maintainers or on a python specific board.
Best of luck to you.
You can not use mysqlclient‑1.4.5‑cp27‑cp27m‑win_amd64.whl as you found in other answer, because that is a wheel for Windows and you are on Linux.
The project mysqlclient, which is a dependency of flask-mysqldb, does not distribute wheels for Linux, so you have to build from source code directly (mysqlclient-1.4.5.tar.gz).
Your install likely fails due to missing build dependencies.
As the project landing page has mentioned, install the build dependencies first:
sudo apt-get install python-dev default-libmysqlclient-dev libssl-dev
Then try again to pip install --user flask-mysqldb.
I followed instructions from this answer mysql_config not found when installing mysqldb python interface
Just in case I ran
sudo apt-get install mysql-server
later
sudo apt-get install libmysqlclient-dev
and finally
pip3 install flask-mysqldb
and it worked, with Ubuntu 18.04