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
Answer from vidarlo on askubuntu.comYou'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
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.
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
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.
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.
» pip install Flask-MySQLdb
Have you tried to set the interpreter in VSCode to correct value (including your virtualenv if you have one)?
Open the command palette (Ctrl-Shift-P) and choose "Python: Select Interpreter".
For more details, see: https://code.visualstudio.com/docs/python/environments
I have had this problem several times and it often works with just "exiting" VSCode since VSCode sometimes doesnt check the path. It uses the "old" settings or "path". By exiting the program and restarting it it updates this and it works (if its this problem)