Python 3.1.1中的搁置模块

时间:2011-07-19 13:42:02

标签: python shelve

我是Python新手,通过O'Reilly“学习Python”系列学习。我被困在一个搁置的例子,无法弄清楚为什么程序不起作用。我正在尝试构建一些示例数据,然后将其加载到搁置文件中。奇怪的是,当我在IDLE shell中键入它时它会起作用,但是当我将它键入.py文件并尝试运行它时它就不行。这是我的代码:

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()

同样,当我在IDLE shell上运行此代码时,我没有问题,但是当我从.py文件运行时出现以下错误:

  

追踪(最近一次通话):    文件“Documents / Python_Learning / shelve.py”,第7行,in      进口货架    文件“Documents / Python_Learning / shelve.py”,第9行,in      db = shelve.open('persondb')   AttributeError:'module'对象没有属性'open'

如果它有帮助,这里是关于我在Snow Leopard上运行的Python版本的信息:

Python 3.1.1(r311:74543,2009年8月24日,18:44:04) [dclwin上的[GCC 4.0.1(Apple Inc. build 5493)]

感谢您对新手的帮助!

威尔

1 个答案:

答案 0 :(得分:7)

将您的模块重命名为shelve.py之外的其他内容 - 您自己导入。