Issue: ImproperlyConfigured: MySQLdb must be installed.
Author: Casyfill
State: closed
Created: 2014-07-10 09:58:29

I am getting this any time I am trying to create a table or do something with connected DB,
althrough I installed MySQL-python lib and it looks working


Hi

Are you using Python3?  This may be a case for PyMySQL.

Maybe the error message should be edited.


no, python 2.7


I installed PyMySQL already – still same issue(


This is not a peewee bug. You need to install one of the mysql python drivers:
- mysql-python
- pymysql

You can verify one of them is installed by running either:
- import MySQLdb
- import pymysql


I have both installed and passing this 'import test'


same error     

```
raise ImproperlyConfigured('MySQLdb or PyMySQL must be installed.')
peewee.ImproperlyConfigured: MySQLdb or PyMySQL must be installed.
>>> 
>>> 
>>> import pymysql
>>>
```


This is the code, I'm not sure how it's failing:

``` python
try:
    import MySQLdb as mysql  # prefer the C module.
except ImportError:
    try:
        import pymysql as mysql
    except ImportError:
        mysql = None
```

Then later...

``` python
class MySQLDatabase(Database):
   ...
    def _connect(self, database, **kwargs):
        if not mysql:
            raise ImproperlyConfigured('MySQLdb or PyMySQL must be installed.')

```


If you want to run that exact code and let me know the output that'd help to debug.

You can also try:  `from peewee import mysql; print mysql` and share the output.


I have also pysqlite, but it always says , "peewee.ImproperlyConfigured: pysqlite or sqlite3 must be installed. "

Below is the list of PIP
antiorm (1.2.1)
blinker (1.4)
cffi (1.7.0)
click (6.6)
ConfigArgParse (0.10.0)
cryptography (1.4)
db (0.1.1)
db-sqlite3 (0.0.1)
enum34 (1.1.6)
Flask (0.11.1)
Flask-Compress (1.3.0)
Flask-Cors (2.1.2)
Flask-MySQL (1.3)
flask-peewee (0.6.7)
future (0.15.2)
geopy (1.11.0)
gpsoauth (0.3.0)
idna (2.1)
ipaddress (1.0.16)
itsdangerous (0.24)
Jinja2 (2.8)
MarkupSafe (0.23)
MySQL-python (1.2.5)
mysql-replication (0.9)
mysqli (0.5.3)
neopysqlite (0.2.2)
peewee (2.8.1)
pip (8.1.2)
protobuf (2.6.1)
protobuf-to-dict (0.1.0)
pyasn1 (0.1.9)
pycparser (2.14)
pycryptodomex (3.4.2)
PyMySQL (0.7.5)
PyMySQL3 (0.5)
pymysqlblinker (1.2)
pymysqlrpc (0.1.4)
pyOpenSSL (16.0.0)
pysqlite (2.8.2)
python-dateutil (2.5.3)
requests (2.10.0)
s2sphere (0.2.4)
setuptools (25.1.1)
six (1.10.0)
sqlist (0.2.1b0)
SQLite3Database (0.2.0)
termcolor (1.1.0)
tomdb (0.5.1)
Werkzeug (0.11.10)
wheel (0.29.0)
wtf-peewee (0.2.6)
WTForms (2.1)

I have tried many package, but it dose not work.


My Code is very simple

`#!/usr/bin/python

# -_\- coding: utf-8 -_-

import logging
import logging.handlers
from peewee import SqliteDatabase,Model, InsertQuery, IntegerField, CharField, \
    FloatField, BooleanField, DateTimeField

LOG_FILE = 'tst.log'
handler = logging.handlers.RotatingFileHandler(LOG_FILE, maxBytes=1024*1024, backupCount=5)
 # 实例化handler
fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'

formatter = logging.Formatter(fmt)   # 实例化formatter
handler.setFormatter(formatter)      # 为handler添加formatter
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logging.getLogger('').addHandler(console)

logger = logging.getLogger('test')  
logger.addHandler(handler)  
logger.setLevel(logging.DEBUG)

db = SqliteDatabase("peewee_db_learn.db")
print db
logger.info("In die Software")

class TestModel(Model):
    class Meta:
        database = db
        order_by = ('-num_id',)

```
num_id = IntegerField(primary_key=True)
name = CharField()
time = DateTimeField()
```

def create_db():
    logger.info("PeeWee Connection")
    db.connect()
    db.create_table(TestModel, safe=True)
    db.close()
    logger.info("PeeWee Finished")

if **name** == '**main**':
    create_db()
`

The error is:

`
Traceback (most recent call last):
  File "pwee.py", line 49, in <module>
    create_db()
  File "pwee.py", line 43, in create_db
    db.connect()
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 3402, in connect
    **self.connect_kwargs)
  File "/usr/local/lib/python2.7/site-packages/peewee.py", line 3657, in _connect
    raise ImproperlyConfigured('pysqlite or sqlite3 must be installed.')
peewee.ImproperlyConfigured: pysqlite or sqlite3 must be installed.
`

and now I have no idea, please help me . Thanks very much.


Well, apparently you don't have SQLite installed. If you're going to use
the SQLiteDatabase class, then you need SQLite bindings installed.
Typically these are part of the normal python installation, but I'm not
sure what kind of system you're running on so perhaps your Python was
compiled without SQLite.

It's an easy fix, though, just do:

pip install pysqlite

Or you can install my fork of pysqlite ;)

pip install -e git+https://github.com/coleifer/pysqlite#egg=pysqlite

Charles

On Sat, Jul 30, 2016 at 12:48 PM, Kevin Hao notifications@github.com
wrote:

> My Code is very simple
> 
> `#!/usr/bin/python
> -_\- coding: utf-8 -_-
> 
> import logging
> import logging.handlers
> from peewee import SqliteDatabase,Model, InsertQuery, IntegerField,
> CharField, \
> FloatField, BooleanField, DateTimeField
> 
> LOG_FILE = 'tst.log'
> handler = logging.handlers.RotatingFileHandler(LOG_FILE,
> maxBytes=1024*1024, backupCount=5)
> 
> # 实例化handler
> 
> fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'
> 
> formatter = logging.Formatter(fmt) # 实例化formatter
> handler.setFormatter(formatter) # 为handler添加formatter
> console = logging.StreamHandler()
> console.setLevel(logging.INFO)
> logging.getLogger('').addHandler(console)
> 
> logger = logging.getLogger('test')
> 
> logger.addHandler(handler)
> 
> logger.setLevel(logging.DEBUG)
> 
> db = SqliteDatabase("peewee_db_learn.db")
> print db
> logger.info("In die Software")
> 
> class TestModel(Model):
> class Meta:
> database = db
> order_by = ('-num_id',)
> 
> num_id = IntegerField(primary_key=True)
> name = CharField()
> time = DateTimeField()
> 
> def create_db():
> logger.info("PeeWee Connection")
> db.connect()
> db.create_table(TestModel, safe=True)
> db.close()
> logger.info("PeeWee Finished")
> 
> if _name_ == '_main_':
> create_db()
> `
> 
> The error is:
> 
> Traceback (most recent call last):
> File "pwee.py", line 49, in <module>
> create_db()
> File "pwee.py", line 43, in create_db
> db.connect()
> File "/usr/local/lib/python2.7/site-packages/peewee.py", line 3402, in
> connect
> **self.connect_kwargs)
> File "/usr/local/lib/python2.7/site-packages/peewee.py", line 3657, in
> _connect
> raise ImproperlyConfigured('pysqlite or sqlite3 must be installed.')
> peewee.ImproperlyConfigured: pysqlite or sqlite3 must be installed.
> 
> and now I have no idea, please help me . Thanks very much.
> 
> —
> You are receiving this because you modified the open/close state.
> Reply to this email directly, view it on GitHub
> https://github.com/coleifer/peewee/issues/390#issuecomment-236379147,
> or mute the thread
> https://github.com/notifications/unsubscribe-auth/AAHUpnMKSCkjxn279qFXy11v3WkvP-L_ks5qa45xgaJpZM4CL55a
> .


Sorry, I read your post a bit more closely, and I see now that you have
installed pysqlite 2.8.2. You can uninstall all that bullshit like
neopysqlite (whatever the hell that is) and Sqlite3Database.

In your virtualenv, try activating it (assuming you are comfortable working
with virtualenv), running python and typing:

> > > from pysqlite2 import dbapi2 as sqlite3
> > > conn = sqlite3.connect(':memory:')

Do you get an ImportError? If you do, then something is wrong with your
installation of pysqlite.

If you do not get an ImportError, try the following:

> > > from peewee import sqlite3
> > > sqlite3  # just type the name of the module and press "Enter".

What does it show for your sqlite3?  Here's an example from one of my
virtualenvs where I've got a custom version of pysqlite:

> > > from peewee import sqlite3
> > > sqlite3
> > > <module 'pysqlite2.dbapi2' from
> > > '/home/code/envs/media_manager/lib/python2.7/site-packages/pysqlite2/dbapi2.pyc'>

Let me know, thanks!

On Sat, Jul 30, 2016 at 1:42 PM, Charles Leifer coleifer@gmail.com wrote:

> Well, apparently you don't have SQLite installed. If you're going to use
> the SQLiteDatabase class, then you need SQLite bindings installed.
> Typically these are part of the normal python installation, but I'm not
> sure what kind of system you're running on so perhaps your Python was
> compiled without SQLite.
> 
> It's an easy fix, though, just do:
> 
> pip install pysqlite
> 
> Or you can install my fork of pysqlite ;)
> 
> pip install -e git+https://github.com/coleifer/pysqlite#egg=pysqlite
> 
> Charles
> 
> On Sat, Jul 30, 2016 at 12:48 PM, Kevin Hao notifications@github.com
> wrote:
> 
> > My Code is very simple
> > 
> > `#!/usr/bin/python
> > -_\- coding: utf-8 -_-
> > 
> > import logging
> > import logging.handlers
> > from peewee import SqliteDatabase,Model, InsertQuery, IntegerField,
> > CharField, \
> > FloatField, BooleanField, DateTimeField
> > 
> > LOG_FILE = 'tst.log'
> > handler = logging.handlers.RotatingFileHandler(LOG_FILE,
> > maxBytes=1024*1024, backupCount=5)
> > 
> > # 实例化handler
> > 
> > fmt = '%(asctime)s - %(filename)s:%(lineno)s - %(name)s - %(message)s'
> > 
> > formatter = logging.Formatter(fmt) # 实例化formatter
> > handler.setFormatter(formatter) # 为handler添加formatter
> > console = logging.StreamHandler()
> > console.setLevel(logging.INFO)
> > logging.getLogger('').addHandler(console)
> > 
> > logger = logging.getLogger('test')
> > 
> > logger.addHandler(handler)
> > 
> > logger.setLevel(logging.DEBUG)
> > 
> > db = SqliteDatabase("peewee_db_learn.db")
> > print db
> > logger.info("In die Software")
> > 
> > class TestModel(Model):
> > class Meta:
> > database = db
> > order_by = ('-num_id',)
> > 
> > num_id = IntegerField(primary_key=True)
> > name = CharField()
> > time = DateTimeField()
> > 
> > def create_db():
> > logger.info("PeeWee Connection")
> > db.connect()
> > db.create_table(TestModel, safe=True)
> > db.close()
> > logger.info("PeeWee Finished")
> > 
> > if _name_ == '_main_':
> > create_db()
> > `
> > 
> > The error is:
> > 
> > Traceback (most recent call last):
> > File "pwee.py", line 49, in <module>
> > create_db()
> > File "pwee.py", line 43, in create_db
> > db.connect()
> > File "/usr/local/lib/python2.7/site-packages/peewee.py", line 3402, in
> > connect
> > **self.connect_kwargs)
> > File "/usr/local/lib/python2.7/site-packages/peewee.py", line 3657, in
> > _connect
> > raise ImproperlyConfigured('pysqlite or sqlite3 must be installed.')
> > peewee.ImproperlyConfigured: pysqlite or sqlite3 must be installed.
> > 
> > and now I have no idea, please help me . Thanks very much.
> > 
> > —
> > You are receiving this because you modified the open/close state.
> > Reply to this email directly, view it on GitHub
> > https://github.com/coleifer/peewee/issues/390#issuecomment-236379147,
> > or mute the thread
> > https://github.com/notifications/unsubscribe-auth/AAHUpnMKSCkjxn279qFXy11v3WkvP-L_ks5qa45xgaJpZM4CL55a
> > .


@Casyfill Thanks for your answer, but in my server I have installed the MySQL-python. It seems not to work for me. And I program it not for managing MySQL ,instead of Sqlite. I have tried for 1 day, but still not solved. Thanks. 


@Casyfill Now I will follow your advice to create a table.  Then , tell you the results. Thanks.


@Casyfill I have tried to create a table , but it still does not work.


@seriousdude No , I am programing with Python 2.7 


I have solved the problem. Before I compiled the Python 2.7, I have not still installed the Sqlite. There is no sqlite.so. Thanks.


needs to `pip install PyMySQL`
in python3.5.2

that fixed my problem, thanks @seriousdude 

Hi, first let me thank you for all the hard work. Thanks!
I'm facing this same issue on an ubuntu server, production deploy. But not all the times, I got it for most of reuquests for my Flask app, but for some some others it does work.

Its on a virtualenv python 3.5.2, I have installed pymysql and imports work, I also got peewee working just fine on separate anaconda 3.6 environment.

Not sure how to debug this, as the modules and all this threads point that I'm doing OK.

Thanks in advice ! 



@laureanoarcanio -- that sounds very much like a deployment issue. Peewee needs to be installed in the same virtual environment as pymysql in order to work correctly.

Hi,
I have bumped into the same issue today, except with psycopg2.
I have deployed a Flask application with apache mod_wsgi, with virtualenv.
Running `from peewee import psycopg2` from console is successful.
But when accessed through the web application peewee raises `ImproperlyConfigured: psycopg2 must be installed.`


You're surely running your app using a different python-path (sys.path) than when you run your console.

The wsgi starts the same venv, and everything is visible from there. It turns out, the actual error when importing pycopg2 is this:
`ImportError: /home/sysadm/customizer/venv/local/lib/python2.7/site-packages/psycopg2/_psycopg.so: wrong ELF class: ELFCLASS32`

Aha, thanks @asztalosdani -- hope you got things working.