Friday, November 10, 2017

[Flask][Resolve] no module named 'flask_mysql'


 Error message

app.py:2: ExtDeprecationWarning: Importing flask.ext.mysql is deprecated, use fl
ask_mysql instead.
  from flask.ext.mysql import MySQL
C:\Users\xxxxxx\AppData\Local\Programs\Python\Python36-32\lib\site-packages\fl
ask\exthook.py:106: ExtDeprecationWarning: Detected extension named flaskext.mys
ql, please rename it to flask_mysql. The old form is deprecated.
  .format(x=modname), ExtDeprecationWarning
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Jan/2018 14:49:11] "GET / HTTP/1.1" 200 -

 Source code

from flask import Flask, render_template, json, request
from flask.ext.mysql import MySQL

mysql = MySQL()
app = Flask(__name__)
mysql.init_app(app)

@app.route('/')
def main():
    return render_template('index.html')
  
if __name__ == "__main__":
    app.run(port=5002)
Error message shows "flask.ext.mysql is deprecated, use flask_mysql instead.", followed the instruction use flask_mysql still fail :

 Wrong code follow instrument from error message:
from flask import Flask, render_template, json, request
from flask_mysql import MySQL

mysql = MySQL()
app = Flask(__name__)
mysql.init_app(app)

@app.route('/')
def main():
    return render_template('index.html')
 
if __name__ == "__main__":
    app.run(port=5002)
Finally find need to import flaskext.mysql:
from flask import Flask, render_template, request, json
from flaskext.mysql import MySQL

app = Flask(__name__)
mysql = MySQL()
mysql.init_app(app)

@app.route("/")
def main():
    return render_template('index.html')

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

Reference

http://flask-mysql.readthedocs.io/en/latest/
https://github.com/jay3dec/PythonFlaskMySQLApp---Part-1

No comments :

Post a Comment