Hacking OS X’s Python dbhash and bsddb modules to work

By default on my Leopard system, the dbhash and bsddb Python modules cannot be loaded.

marc@hyperion:~$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 21:08:09)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbhash
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.5
        /lib/python2.5/dbhash.py", line 5, in <module>
    import bsddb
  File "/System/Library/Frameworks/Python.framework/Versions/2.5
        /lib/python2.5/bsddb/__init__.py", line 51, in <module>
    import _bsddb
ImportError: No module named _bsddb

I managed to get it to work though by installing bsddb3 from the pybsddb site and hacking the file /System/Library/Frameworks/Python.framework/Versions/2.5
/lib/python2.5/dbhash.py
:

marc@hyperion:~$ diff -u dbhash.py.orig dbhash.py
--- dbhash.py.orig      2007-11-28 10:16:33.000000000 -0800
+++ dbhash.py   2007-11-28 10:36:52.000000000 -0800
@@ -2,7 +2,7 @@

 import sys
 try:
-    import bsddb
+    import bsddb3 as bsddb
 except ImportError:
     # prevent a second import of this module from spuriously succeeding
     del sys.modules[__name__]

Here’s the patch.

Now it works:

marc@hyperion:~$ python
Python 2.5.1 (r251:54863, Oct  5 2007, 21:08:09)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import dbhash
>>> dir(dbhash)
['__all__', '__builtins__', '__doc__', '__file__', '__name__', 'bsddb',
'error', 'open', 'sys']