Flask Error Note: Importerror:no module named ' MySQLdb '

Source: Internet
Author: User

Environment Description: WINDOWS10
Deploy a flask Project yourself

#/usr/bin/python env#coding:utf8from flask_script Import managerfrom flask_bootstrap import Bootstrapfrom flask_moment  Import momentfrom datetime import datetimefrom FLASK_WTF import flaskformfrom wtforms import Stringfield, Submitfieldfrom Wtforms.validators Import requiredfrom flask import Flask, render_template, session, redirect, Url_for,flashimport os,py Mysqlfrom flask_sqlalchemy Import sqlalchemypymysql.install_as_mysqldb () app = Flask (__name__) app.config[' SECRET_KEY '] = ' hard to guess string ' basedir = Os.path.abspath (Os.path.dirname (__file__)) app.config[' sqlalchemy_database_uri '] = ' Mysql://root: Password @ip:3306/school ' app.config[' sqlalchemy_commit_on_teardown '] = trueapp.config[' SQLALCHEMY_TRACK_ Modifications '] = Falsemanager = Manager (APP) bootstrap = bootstrap (APP) moment = Moment (APP) db = SQLAlchemy (APP) class Role (Db. Model): __tablename__ = ' roles ' id = db. Column (db. Integer,primary_key=true) name = db. Column (db. String (unique=true), user = Db.relationship (' user ', Backref= ' role ', lazy= ' dynamic ') def __repr__ (self): return ' <role {}> '. Format (Self.name) class User (db. Model): __tablename__ = ' users ' id = db. Column (db. Integer,primary_key=true) Username = db. Column (db. String (UP), unique=true,index=true) role_id = db. Column (db. Integer,db. ForeignKey (' roles.id ') def __repr__ (self): return ' <user {}> '. Format (Self.username) class Nameform (Flaskfo RM): name = Stringfield (' What's your name? ', validators=[required ()]) Submit = Submitfield (' Submit ') @app. Errorhandl ER (404) def page_not_found (e): Return render_template (' 404.html '), 404@app.errorhandler (+) def internal_server_ Error (E): Return render_template (' 500.html '), 500@app.route ('/', methods=[' GET ', ' POST ']) def index (): Form = Nameform () if Form.validate_on_submit (): Old_name = session.get (' name ') if Old_name is not None and old_name! = F        Orm.name.data:flash (' Looks like you have changed your name! ') session[' name '] = form.Name.data return Redirect (Url_for (' index ')) return render_template (' index.html ', Form=form,name=session.get (' name '), Current_time=datetime.utcnow ()) If __name__ = = ' __main__ ': app.run (debug = true,host= ' 0.0.0.0 ', port=9000)
访问:


Execution Error:

Traceback (Recent): File "manage.py", line +, in <module> manager.run () file "C:\Python27\lib\s ite-packages\flask_script\__init__.py ", line 412, in run result = Self.handle (Sys.argv[0], sys.argv[1:]) File" C:\Pyth on27\lib\site-packages\flask_script\__init__.py ", line 383, in handle res = handle (*args, **config) File" C:\Python27\ lib\site-packages\flask_script\commands.py ", line 216, in __call__ return Self.run (*args, **kwargs) File" C:\Python27\   lib\site-packages\flask_migrate\__init__.py ", line 235, in Upgrade Command.upgrade (config, revision, sql=sql, Tag=tag) File "C:\Python27\lib\site-packages\alembic\command.py", line 174, in Upgrade script.run_env () file "C:\Python27\lib \site-packages\alembic\script\base.py ", line 397, in Run_env util.load_python_file (Self.dir, ' env.py ') file" C:\Python 27\lib\site-packages\alembic\util\pyfiles.py ", line, Load_python_file module = load_module_py (module_id, Path) F Ile "C:\Python27\lib\site-packages\alembic\util\compat.py ", line +, in load_module_py mod = Imp.load_source (module_id, Path, FP) File" Migrati ons\env.py ", line, in <module> run_migrations_online () File" migrations\env.py ", line +, in Run_migrations_o Nline Poolclass=pool. Nullpool) File "C:\Python27\lib\site-packages\sqlalchemy\engine\__init__.py", line 427, in Engine_from_config return C Reate_engine (URL, **options) File "C:\Python27\lib\site-packages\sqlalchemy\engine\__init__.py", line 386, in Create_ Engine return Strategy.create (*args, **kwargs) File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py" , line page, in Create Dbapi = Dialect_cls.dbapi (**dbapi_args) File "C:\Python27\lib\site-packages\sqlalchemy\dialects\m ysql\mysqldb.py ", line, Dbapi return __import__ (' MySQLdb ') importerror:no module named MySQLdb

Search the Web for the following solutions:

1, installation Pymysql
$ pip Install Pymysql
Collecting Pymysql
Downloading PYMYSQL-0.7.11-PY2.PY3-NONE-ANY.WHL (78kB)
Installing collected Packages:pymysql
Successfully installed PyMySQL-0.7.11

2. Import Module Pymysql

Import Pymysql
Pymysql.install_as_mysqldb ()

Successful visit: http://127.0.0.1:9000

Flask Error Note: Importerror:no module named ' MySQLdb '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.