Question

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.

Was it helpful?

Solution 2

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
)

OTHER TIPS

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top