python shelve ... bsddb 已弃用 ... 如何让 shelve 使用另一个数据库?

发布于 2024-12-06 02:48:42 字数 372 浏览 5 评论 0原文

我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度℉ 2024-12-13 02:48:42

您可以在调用 shelve.open 之前通过设置 anydbm._defaultmod 来设置创建的数据库的类型。

这适用于 Python 2.6(也许也适用于 2.7?),但由于 anydbm._defaultmod 是一个私有变量,请注意这是一个 hack。

anydbm._defaultmod=__import__('gdbm')

例如:

import anydbm
import whichdb
import contextlib

anydbm._defaultmod=__import__('gdbm')
filename='/tmp/shelf.dat'
with contextlib.closing(shelve.open(filename)) as f: pass
result=whichdb.whichdb(filename)

print(result)
# gdbm

You can set the type of db created by setting anydbm._defaultmod before calling shelve.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.

anydbm._defaultmod=__import__('gdbm')

For example:

import anydbm
import whichdb
import contextlib

anydbm._defaultmod=__import__('gdbm')
filename='/tmp/shelf.dat'
with contextlib.closing(shelve.open(filename)) as f: pass
result=whichdb.whichdb(filename)

print(result)
# gdbm
妞丶爷亲个 2024-12-13 02:48:42

我好像问错了问题。在构建 Windows exe 时,py2exe 不包含 dbm 模块(它无法推断出这种依赖关系),因此在运行时 python 绝望地试图找到 bdbm 模块。

此脚本 setup.py 包含一个模块,该模块使 py2exe 版本的行为就像正常运行的版本一样。它包括一个 dbm-clone 模块(我只存储十个简单的字典,所以基本的 dumbdbm 模块就足够了

from distutils.core import setup
import py2exe, sys, os
from glob import glob

sys.argv.append('py2exe')
data_files = [("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
setup(
    data_files=data_files,
    windows = ["cashflowSim.py"],
    options={
       "py2exe":{"includes":["dumbdbm"]}},
       zipfile = None
)

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

from distutils.core import setup
import py2exe, sys, os
from glob import glob

sys.argv.append('py2exe')
data_files = [("Microsoft.VC90.CRT", glob(r'C:\Program Files\Microsoft Visual Studio 9.0\VC\redist\x86\Microsoft.VC90.CRT\*.*'))]
setup(
    data_files=data_files,
    windows = ["cashflowSim.py"],
    options={
       "py2exe":{"includes":["dumbdbm"]}},
       zipfile = None
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文