Peewee (Python Sqlite ORM) – NameError: name 'SqliteDatabase' is not defined

Question:

OS Linux Mint 18.3 (but same problem also with version 19)
Python3 and Sqlite3 installed

After a lot of trouble with “pip / pip3”, I managed to install Peewee.

I tried running the following sample script with python3 peewee.py but I get this error:

SCRIPT (peewee.py)

from peewee import *

db = SqliteDatabase("people.db")

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

    class Meta:
        database = db # This model uses the "people.db" database.

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

    class Meta:
        database = db # this model uses the "people.db" database

db.connect()

ERROR

Traceback (most recent call last):
  File "peewee.py", line 3, in <module>
    from peewee import *
  File "/home/.../peewee.py", line 6, in <module>
    db = SqliteDatabase("people.db")
NameError: name 'SqliteDatabase' is not defined

I’ve already done an extensive research on google / StackOverflow, but I can’t solve this problem. Could you please help me?

Asked By: Igor Carmagna

||

Answers:

I tried a different approach to the problem… Turns out the problem isn’t related to peewee specifically but to python.

I named the script file peewee.py.

So, because of the first line of the script from peewee import *, Python imports my own script instead of the real peewee package, hence the error.

SOLUTION
Rename the script file to something else.

(Comment: … So sad… a lot of time wasted for a silly newbie error)

Source:
Python AttributeError: 'module' object has no attribute 'connect'

Answered By: Igor Carmagna

This problem appears in vscode
But it does not appear in pycharm
Replace the total include-me-library with include-each-item alone

Answered By: lakhdar bekkari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.