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

9 thoughts on “Hacking OS X’s Python dbhash and bsddb modules to work

  1. 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. 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. 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. 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__]

  5. Pingback: sudo easy_install bsddb3 error :Can't find a local Berkeley DB installation | BlogoSfera

  6. Pingback: Installing bsddb package - python | BlogoSfera

  7. I noticed that you posted this problem about Python 2.5 more than six years ago.

    I just downloaded a brand new Anaconda installation, and I seem to be getting the same issue in Python 2.7.5:

    File “//anaconda/lib/python2.7/bsddb/__init__.py”, line 67, in
    import _bsddb
    ImportError: No module named _bsddb

    Is it possible that this issue really hasn’t been fixed in 6+ years, or do you think this is a different issue?

    I’m reluctant to muck around in things like this without knowing what I’m doing.

    Thanks!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>