9

I tried to make a MySQL connection with peewee and followed the tutorial from their website: peewee quickstart

So my code is the following:

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()

(My Database is from xampp)

But when i execute this code I get this error message:

peewee.ImproperlyConfigured: MySQL driver not installed!

I tried to fix this by installing this MySQL Driver. But this changed absolutely nothing. Due to me beeing new to python I have no idea what I can do to fix this, if i'm just missing a import or if I have to install a library with pip?

Tamino W.
  • 139
  • 1
  • 1
  • 7

3 Answers3

12

Install a MySQL driver:

pip install pymysql
tread
  • 10,133
  • 17
  • 95
  • 170
5

The docs are clear, as is the error message: http://docs.peewee-orm.com/en/latest/peewee/database.html#using-mysql

Install pymysql or mysqldb.

To use the non-standard mysql-connector driver, you need to import the playhouse.mysql_ext module and use the MySQLConnectorDatabase implementation:

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

coleifer
  • 24,887
  • 6
  • 60
  • 75
0

The docs are not that clear anymore it seems. The problem is indeed a missing MySQL driver. There are various MySQL drivers available for Python, each with their own pros and cons.

This Github comment gives a good summary:

  • pymysql is a pure-python mysql client, works with python 2 and 3. Peewee will use attempt to use pymysql first.
  • mysqlclient uses a c extension and supports python 3. It exposes a MySQLdb module. Peewee will attempt to use this module if pymysql is not installed.
  • mysql-python is also called MySQLdb1 and is legacy and should not be used. Since this shares the same module name as mysqlclient, same applies.
  • mysql-connector python pure-python (I think??) supports python 3. To use this driver you can use playhouse.mysql_ext.MySQLConnectorDatabase.

Some additional notes:

  • pymsql, being a pure-python implementation, is easy to install without many dependencies, but is really slow.
  • mysqlclient is fast, but has to be compiled and thus has additional dependencies. At the time of writing, on Ubuntu, those take up an additional 26 Mb of diskspace, which may be relevant if you're using containers.
  • I haven't tried mysql-connector.
Ferry Boender
  • 668
  • 1
  • 7
  • 14