5

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?

2
  • This 'silly' but very frustrating and understandable issue is now covered by a standard-warning in pylint: "import-self / W0406" pylint.readthedocs.io/en/latest/user_guide/messages/warning/… Commented Sep 24 at 11:05
  • Another way to prevent this is to not use wildcard-imports. Commented Sep 24 at 11:10

2 Answers 2

5

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'

Sign up to request clarification or add additional context in comments.

1 Comment

similar issue for me too - i called my file 'select.py'
0

For me the issue was that I was running Peewee in wsl and installed the pip package there. So the script would run well when I called this tutorial script through the terminal.

But when i pressed the Run button in vs code it would not work and ask SQlitedatabase not found etc.

So I also installed it in windows with pip install peewee. Now both methods work perfect! Hope this helps someone.

1 Comment

That should produce a different error. When the module is not installed, there should be a "ModuleNotFoundError"; but for OP, import 'succeeds' but using names from the real module fails because the real module was never imported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.