I am running into an issue with my flask web server. I get an error message once I add from flask_mysqldb import MySQL as well as the SQL script into my source file. I get this message:
$ python app.py Traceback (most recent call last): File "app.py", line 3, in 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 import MySQLdb File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/init.py", line 18, in import _mysql ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so Reason: image not found**
I am wondering if it has to do with me using python 3.7. The flask mysqldb doc says that it supports Python 2.7, 3.4 and 3.5. Should I be using an older version of python? Your input is much apprenticed!
Here is the source code:
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
from data import Articles
from flask_mysqldb import MySQL
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from passlib.hash import sha256_crypt
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = '123456'
app.config['MYSQL_DB'] = 'myflaskapp'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql = MySQL(app)
Articles = Articles()
@app.route("/")
@app.route("/home")
def home():
return render_template('home.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/articles')
def articles():
return render_template('articles.html', articles = Articles)
@app.route('/article/<string:id>/')
def article(id):
return render_template('article.html', id = id)
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('Confirm', message='Passwords do not match')
])
confirm = PasswordField('Confirm Password')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
# Create cursor
cur = mysql.connection.cursor()
# Execute query
cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s )", (name, email, username, password))
#Commit to DB
mysql.connection.commit()
#close connection
cur.close()
flash('You are now registered and can now log in', 'success')
redirect(url_for('index'))
return render_template('register.html', form=form)
if __name__ == '__main__':
app.run(debug=True)
I've been following the same programming tutorial as you and I got everything working using Python 3.7. The only difference is that I'm using Linux and you're using MacOS. The problem here is not Python 3.7. Instead, the problem is indicated by this snippet:
The problem is that the MySQL package can't find the dynamic library
_mysql.cpython-37m-darwin.so
as discussed in another thread.The dynamic library should be available in the
/usr/local/mysql/lib
directory. If it is in that directory, then you just need to modify the$DYLD_LIBRARY_PATH
variable with the following command:export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH
.