3

I'm quite new to Python and thus also 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?

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. I get another error (NameError: name 'Model' is not defined) when only using Model

  • I have updated my peewee to newest version (sudo pip intall -U peewee)

  • I have tried to run a print dir(peewee) which gives me ['__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.

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

I found the script at http://peewee.readthedocs.org/en/latest/peewee/cookbook.html

Fizk
  • 1,015
  • 1
  • 7
  • 19

1 Answers1

7

You have named your file "/home/ubuntu/python/test/peewee.py" so Python is trying to import from that instead of the peewee module.

Don't use file names that mirror the names of your Python modules. Just rename your "/home/ubuntu/python/test/peewee.py" file to something else and it will work.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I have developed a habit of saving stuff I'm working on under filenames that are not legal Python identifiers. For example, include a hyphen in the filename. This is perfectly legal to run in Python, but it can't be imported, so it will never shadow a built-in module's name. – kindall Jun 20 '14 at 21:27