簡體   English   中英

Peewee(Python Sqlite ORM)-NameError:名稱'SqliteDatabase'未定義

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

OS Linux Mint 18.3(但版本19也存在相同問題)
安裝了Python3和Sqlite3

在“ pip / pip3”遇到很多麻煩之后,我設法安裝了Peewee

我嘗試使用python3 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()

錯誤

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

我已經對google / StackOverflow進行了廣泛的研究,但無法解決此問題。 請你幫助我好嗎?

我嘗試了另一種方法來解決該問題...原來,該問題與peewee無關,而與python有關。

我將腳本文件命名為peewee.py。

因此,由於from peewee import *腳本的第一行,Python會導入我自己的腳本而不是真正的peewee包,因此會出現錯誤。


將腳本文件重命名為其他名稱。

(評論:...真傷心...浪費大量時間浪費在一個愚蠢的新手錯誤上)

資源:
Python AttributeError:“模塊”對象沒有屬性“連接”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM