3

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.

Tim Richardson
  • 6,608
  • 6
  • 44
  • 71

2 Answers2

3

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
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • The problem I have is on windows, using ActiveState python, and trying to make a windows executable using py2exe. The executable fails because the use of shelve tries to load bsddb. When I run the program as python program.py it works. So I guess I have another problem. Using your hack above made no difference. – Tim Richardson Sep 23 '11 at 02:11
  • I delete the shelve database to avoid any problems. I found a 2004 posting with the tweak I need to get py2exe working: I need to explicitly include a dbm clone. So I include dubmdbm via options={ "py2exe":{"includes":["dumbdbm"]}}, in the setup.py script. – Tim Richardson Sep 23 '11 at 15:59
  • Ah, I see. I'm glad you solved the problem. Perhaps add an answer, and I'll delete mine. – unutbu Sep 23 '11 at 16:09
  • Actually, you answered the question, but it turns out I had the wrong question in the end. – Tim Richardson Sep 23 '11 at 21:25
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
)
Tim Richardson
  • 6,608
  • 6
  • 44
  • 71