Python peewee.ImproperlyConfigured:未安装 MySQL 驱动程序

Tam*_* W. 10 python mysql-python peewee

我尝试与 peewee 建立 MySQL 连接,并按照他们网站上的教程进行操作: peewee 快速入门

所以我的代码如下:

from peewee import *

db = MySQLDatabase(
    host='127.0.0.1',
    user='root',
    password='',
    database='db_test'
)

class Person(Model):
    name = CharField()
    birthday = DateField()

    class Meta:
        database = db

class Pet(Model):
    owner = ForeignKeyField(Person, backref='pets')
    name = CharField()
    animal_type = CharField()

    class Meta:
        database = db

db.connect()

db.create_tables([Person, Pet])

db.close()
Run Code Online (Sandbox Code Playgroud)

(我的数据库来自xampp)

但是当我执行此代码时,我收到此错误消息:

peewee.ImproperlyConfigured:MySQL 驱动程序未安装!

我尝试通过安装 MySQL 驱动程序来解决此问题。但这完全没有改变。由于我是 python 新手,我不知道如何解决这个问题,如果我只是缺少导入或者我必须使用 pip 安装库?

sur*_*190 13

安装 MySQL 驱动程序:

pip install pymysql
Run Code Online (Sandbox Code Playgroud)


col*_*fer 6

文档很清楚,错误消息也是如此:http://docs.peewee-orm.com/en/latest/peewee/database.html#using-mysql

安装 pymysql 或 mysqldb。

要使用非标准 mysql-connector 驱动程序,需要导入模块playhouse.mysql_ext并使用MySQLConnectorDatabase实现:

http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#mysql-ext