简体   繁体   English

Peewee(Python Sqlite ORM)-NameError:名称'SqliteDatabase'未定义

[英]Peewee (Python Sqlite ORM) - NameError: name 'SqliteDatabase' is not defined

OS Linux Mint 18.3 (but same problem also with version 19) OS Linux Mint 18.3(但版本19也存在相同问题)
Python3 and Sqlite3 installed 安装了Python3和Sqlite3

After a lot of trouble with "pip / pip3", I managed to install Peewee . 在“ pip / pip3”遇到很多麻烦之后,我设法安装了Peewee

I tried running the following sample script with python3 peewee.py but I get this error: 我尝试使用python3 peewee.py运行以下示例脚本,但出现此错误:

SCRIPT (peewee.py) 脚本(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. 我已经对google / StackOverflow进行了广泛的研究,但无法解决此问题。 Could you please help me? 请你帮助我好吗?

I tried a different approach to the problem... Turns out the problem isn't related to peewee specifically but to python. 我尝试了另一种方法来解决该问题...原来,该问题与peewee无关,而与python有关。

I named the script file peewee.py. 我将脚本文件命名为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. 因此,由于from peewee import *脚本的第一行,Python会导入我自己的脚本而不是真正的peewee包,因此会出现错误。

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' Python AttributeError:“模块”对象没有属性“连接”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM