Issues with Flask-MySQLdb using Python3.7
我的Flask Web服务器出现问题。 从flask_mysqldb添加后,我收到一条错误消息,将MySQL和SQL脚本导入源文件。 我收到此消息:
$ 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**
我想知道是否与我使用python 3.7有关。 烧瓶mysqldb doc表示它支持Python 2.7、3.4和3.5。 我应该使用旧版本的python吗? 您的投入非常多!
这是源代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 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) |
我一直在遵循与您相同的编程教程,并且使用Python 3.7可以正常工作。 唯一的区别是我正在使用Linux,而您正在使用MacOS。 这里的问题不是Python 3.7。 相反,此代码段指示了该问题:
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**
问题是MySQL软件包找不到另一个线程中讨论的动态库
动态库应该在