简体   繁体   English

Python和Peewee中的AttributeError

[英]AttributeError in Python and Peewee

I'm quite new to Python and thus also Peewee. 我对Python和Peewee还是很陌生。

I have a real simple script set up that gives me the error AttributeError: 'module' object has no attribute 'Model' - Can anyone tell me why? 我有一个真正简单的脚本设置,该脚本给我错误AttributeError: 'module' object has no attribute 'Model' Model'-有人可以告诉我为什么吗?

I've got the following script: 我有以下脚本:

import peewee
from peewee import *

print dir(peewee)

class User(peewee.Model):
    username = peewee.CharField()
  • I've tried to make the User class with both peewee.Model and just Model , as I've found some previous question about a similar error, that stated that it could be the problem, but it doesn't seem to be in my case. 我已经尝试使用peewee.ModelModel来创建User类,因为我之前发现了一个类似错误的问题,该问题可能是问题所在,但似乎不是我的问题案件。 I get another error ( NameError: name 'Model' is not defined ) when only using Model 仅使用Model时出现另一个错误( NameError: name 'Model' is not defined

  • I have updated my peewee to newest version ( sudo pip intall -U peewee ) 我已经把我的peewee更新到了最新版本( sudo pip intall -U peewee

  • I have tried to run a print dir(peewee) which gives me ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'peewee'] . 我试图运行一个print dir(peewee) ,该print dir(peewee)为我提供['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'peewee'] I find this a bit odd, since a import math and then dir(math) gives me the functions of math. 我觉得这有点奇怪,因为导入数学然后dir(math)给了我数学功能。

The full stack is 完整的堆栈是

Traceback (most recent call last):
  File "peewee.py", line 1, in <module>
    import peewee
  File "/home/ubuntu/python/test/peewee.py", line 6, in <module>
    class User(Model):
NameError: name 'Model' is not defined

I'm running peewee version 2.2.4 and Python version 2.7.3 我正在运行peewee版本2.2.4和Python版本2.7.3

I found the script at http://peewee.readthedocs.org/en/latest/peewee/cookbook.html 我在http://peewee.readthedocs.org/en/latest/peewee/cookbook.html找到了脚本

You have named your file "/home/ubuntu/python/test/peewee.py" so Python is trying to import from that instead of the peewee module. 您已将文件命名为"/home/ubuntu/python/test/peewee.py"因此Python尝试从中导入而不是peewee模块。

Don't use file names that mirror the names of your Python modules. 不要使用与Python模块名称相同的文件名。 Just rename your "/home/ubuntu/python/test/peewee.py" file to something else and it will work. 只需将您的"/home/ubuntu/python/test/peewee.py"文件重命名为其他文件即可。

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

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