我的Flask Web服务器遇到问题。将从flask_mysqldb导入MySQL 以及SQL脚本添加到源文件中后,出现错误消息。我收到此消息:
$ python app.py Traceback(最近一次调用):文件“ app.py”,第3行,从flask_mysqldb导入MySQL文件 “ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/flask_mysqldb/init.py”, 导入MySQLdb文件中的第1行 “ /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/init.py”, 第18行,在import _mysql ImportError中: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so, 2):库未加载:@ rpath / libmysqlclient.21.dylib引用 从: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so 原因:找不到图片**
我想知道是否与使用python 3.7有关。烧瓶mysqldb doc表示它支持Python 2.7、3.4和3.5。我应该使用旧版本的python吗?您的意见太过刻意了!
以下是源代码:
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)
答案 0 :(得分:1)
我一直在遵循与您相同的编程教程,并且使用Python 3.7可以正常工作。唯一的区别是我正在使用Linux,而您正在使用MacOS。这里的问题不是Python 3.7。相反,此代码段指示了问题:
未加载库:@ rpath / libmysqlclient.21.dylib引用自:/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/_mysql.cpython-37m-darwin.so原因:找不到图片**
问题是MySQL包找不到another thread中讨论的动态库_mysql.cpython-37m-darwin.so
。
动态库应该在/usr/local/mysql/lib
目录中可用。如果在该目录中,则只需使用以下命令修改$DYLD_LIBRARY_PATH
变量:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH
。