python shelve ... bsddb 已弃用 ... 如何让 shelve 使用另一个数据库?
我有一个在 OS X 上用 python 2.7.2 开发的应用程序。 我使用模块 shelve 并且似乎在 mac 上默认为 bsddb。 该程序无法在具有 ActiveState python 2.7 的 Windows 7 计算机上运行,因为模块 bsddb 不存在并且不在 ActiveState 的包管理器 (pypm) 中。 ActiveState 的文档表示在 2.6 版中已弃用。 我猜它会尝试 bdddb,因为创建数据库的 OS X python 默认为 bsddb。 当我删除搁置数据库并在 Windows 上运行它时,它很乐意使用其他一些底层数据库。 Mac的python也很高兴。
所以我认为我应该强制使用非 bdsdb 后端进行搁置。就像 gdbm 模块一样。 但我不知道该怎么做。
I have an app developed in python 2.7.2 on OS X.
I use the module shelve and seems to default to bsddb on the mac.
The program won't run on a Windows 7 machine with ActiveState python 2.7 because the module bsddb is not present and is not in ActiveState's package manager (pypm). ActiveState's documentation says deprecated at v 2.6.
I guess it tries bdddb because the OS X python which created the DB defaults to bsddb.
When I delete the shelve database and run it on Windows, it happily uses some other underlying database. The Mac's python is also happy.
So I think I should enforce the use of a non-bdsdb backend for shelve. Like the gdbm module.
But I can't work out how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在调用
shelve.open
之前通过设置anydbm._defaultmod
来设置创建的数据库的类型。这适用于 Python 2.6(也许也适用于 2.7?),但由于
anydbm._defaultmod
是一个私有变量,请注意这是一个 hack。例如:
You can set the type of db created by setting
anydbm._defaultmod
before callingshelve.open
.This works for Python 2.6 (and maybe for 2.7?), but since
anydbm._defaultmod
is a private variable, be aware that this is a hack.For example:
我好像问错了问题。在构建 Windows exe 时,py2exe 不包含 dbm 模块(它无法推断出这种依赖关系),因此在运行时 python 绝望地试图找到 bdbm 模块。
此脚本 setup.py 包含一个模块,该模块使 py2exe 版本的行为就像正常运行的版本一样。它包括一个 dbm-clone 模块(我只存储十个简单的字典,所以基本的 dumbdbm 模块就足够了
I seemed to have asked the wrong question. When building the windows exe, py2exe was not including an dbm modules (it couldn't infer this dependency), so at runtime python in desperation tried to find the bdbm module.
this script setup.py includes a module which makes the py2exe version behave like the version run normally. It includes a dbm-clone module (I'm only storing ten simple dictionaries so the basic dumbdbm module is good enough