Will Fogel
Will Fogel

Reputation: 81

Shelve module in Python 3.1.1

I'm new to Python and learning through the O'Reilly "Learning Python" series. I'm stuck on a shelve example and can't figure out why the program doesn't work. I'm trying to build some sample data and then load it into a shelve file. The weird thing is that it works when I type it into the IDLE shell but not when I type it into a .py file and try to run it. Here's my code:

from Python_Learning.person import Person, Manager

bob = Person('Bob Smith', 'dev', 60000)
sue = Person('Sue Jones', job = 'designer', pay = 100000)
tom = Manager('Tom Jones', 1000000)

import shelve
db = shelve.open('persondb')
for object in (bob, sue, tom):
    db[object.name] = object
db.close()

Again, when I run this code on an IDLE shell, I have no problem, but when I run from a .py file I get the following error:

Traceback (most recent call last): File "Documents/Python_Learning/shelve.py", line 7, in import shelve File "Documents/Python_Learning/shelve.py", line 9, in db = shelve.open('persondb') AttributeError: 'module' object has no attribute 'open'

In case it helps, here's the info on the Python version I'm running on Snow Leopard:

Python 3.1.1 (r311:74543, Aug 24 2009, 18:44:04) [GCC 4.0.1 (Apple Inc. build 5493)] on darwin

Thanks for your help to a newbie!

Will

Upvotes: 2

Views: 2402

Answers (1)

Cat Plus Plus
Cat Plus Plus

Reputation: 129764

Rename your module to something else than shelve.py — you're importing yourself.

Upvotes: 7

Related Questions