Rocking the blogosphere
Apple Online Store

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']

Del.icio.us Digg Reddit Technorati

Possibly related posts

Comments

  1. January 11th, 2008 | 11:14 am

    Hrm. Long story, but I’m trying to get cvs2svn to work on Leopard, and that requires a working bsddb module.

    I can’t actually get pybsddb to build:
    distutils/sysconfig.py”, line 389, in _init_posix
    raise DistutilsPlatformError(my_msg)
    distutils.errors.DistutilsPlatformError: $MACOSX_DEPLOYMENT_TARGET mismatch: now “10.4″ but “10.5″ during configure

    How did you get on with that part?

  2. May 15th, 2008 | 1:04 pm

    Nice post. I’ve followed your instructions and can import dbhash, but am still getting the following code error:

    dbm = bsddb.hashopen(bdb_name, ‘n’)
    NameError: global name ‘bsddb’ is not defined

    Any advice you have will be much appreciated.

    Thanks,

    -k.

  3. May 15th, 2008 | 1:14 pm

    And actually, the code used to state “import bsddb”, which produces an error itself. “import dbhash” does work w/o problem as you show.

  4. HT
    May 21st, 2008 | 3:45 pm

    Am I missing something or is the post above regarding the _bsddb module hack incomplete? What file did you actually hack below — I don’t understand how the logic flows below and what file you are actually editing…

    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:
    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__]

Leave a reply

Apple Online Store
Apple Online Store