【问题标题】:module object has no attribute open in shelve [duplicate]模块对象没有在搁置中打开的属性[重复]
【发布时间】:2014-11-24 10:30:01
【问题描述】:

以下是我的代码,

import shelve

sd = shelve.open("session.data")

当我在 IDLE 中尝试相同的代码时,我没有收到任何错误。 但是当使用此代码运行脚本时,我收到以下错误,

Traceback (most recent call last):
  File "try.py", line 3, in <module>
    sd = shelve.open("session.data")
AttributeError: 'module' object has no attribute 'open'

【问题讨论】:

  • 您的目录中有shelve.py 吗?
  • 哦,是的。对不起,感谢您的回答。

标签: python windows attributeerror shelve


【解决方案1】:

您导入了一个不同模块shelve,它掩盖了标准库版本。

做:

import shelve
print(shelve.__file__)

然后将该文件移到一边、重命名或删除它。

【讨论】:

  • 我试过这个但对我来说它给出了错误“import shelve my_shelf = shelve.open(filename,'n') # 'n' for new AttributeError: module 'shelve' has no attribute 'open '"
  • @R.Cox:您是否尝试打印__fie__ 属性而不是使用shelve.open()
  • 我运行了“print(shelve.__file__)”。它回复“NameError:名称'搁置'未定义”
  • @R.Cox:我没有说您需要删除 import shelve 行,我认为您仍然会保留它,就像我在回答中显示的那样。另请参阅我将这个问题作为副本关闭的规范帖子以获取更多信息。
  • 当我运行“import shelve”时,按回车键,然后运行“print(shelve.__file__)”它会回复一个以“Continuum\Anaconda3\lib\shelve.py”结尾的位置
猜你喜欢
  • 1970-01-01
  • 2018-07-07
  • 2017-04-22
  • 2015-09-27
  • 2016-05-22
  • 1970-01-01
  • 2017-11-22
  • 2012-07-09
相关资源
最近更新 更多