22

I am totally new to python and I have this message when I try to import bsdddb

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/bsddb/__init__.py", line 67, in <module>
    import _bsddb
ImportError: No module named _bsddb

So I followed this and this, so I downloaded this package bsddb3-4.5.0.tar.gz. What am I suppose to do with it, I tried to run python install setup.py int the bsddb3-4.5.0 in the right directory (I am using an osx). Then I get

Can't find a local BerkeleyDB installation.
(suggestion: try the --berkeley-db=/path/to/bsddb option)

Some one could help ?

Community
  • 1
  • 1
user1611830
  • 4,749
  • 10
  • 52
  • 89

4 Answers4

20

bsddb is deprecated since 2.6. The ideal is to use the bsddb3 module.

My suggestion, and by far the easiest option, is to install Homebrew and use it to get BerkeleyDB on your system:

brew install berkeley-db

After this install bsddb3 using pip

pip install bsddb3

or download the source and install normally.

python setup.py install
Francisco Roque
  • 431
  • 4
  • 14
  • 8
    pip install straight didn't quite work for me, I had to go to the source directory of bsddb3 and do a python setup.py install --berkeley-db=$(brew --prefix)/berkeley-db/5.3.21/ – Chmouel Boudjnah Sep 06 '13 at 08:40
  • Looks like it might have moved again in brew, expanding on Chmouel's comment: `python setup.py install --berkeley-db=\`find $(brew --prefix)/*/berkeley-db/5* -name berk*\`` – Sean Jun 07 '14 at 01:54
  • 8
    This worked for me on Mavericks using `python` from Apple and `pip` and `berkeley-db` from Homebrew (as root): `BERKELEYDB_DIR=$(brew --cellar)/berkeley-db/5.3.28 pip install bsddb3` (for details see http://chriszf.posthaven.com/getting-berkeleydb-working-with-python-on-osx) – Stefan Schmidt Jul 03 '14 at 19:53
  • 3
    Update for 2016: `YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=1 BERKELEYDB_DIR=$(brew --cellar)/berkeley-db/6.1.26 pip install bsddb3`, which worked for me. – mhulse Aug 07 '16 at 00:10
  • 2
    Update for 2018: OSX El Capitan introduces "System Integrity Protection" which means root is no longer "God" and some system directories are simply unwritable. Problem: built-in python's `pip` tries to write to one of these sacred directories so it no longer works. SO, first install [homebrew](https://brew.sh/) and run: `pip install python`. After that, open a new terminal window and you can continue as normal: `sudo YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=1 BERKELEYDB_DIR=$(brew --cellar)/berkeley-db/6.2.32 pip install bsddb3` – Bob Chip Jan 03 '18 at 10:55
  • `bsddb3` is now `berkleydb`: `YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=1 BERKELEYDB_DIR=/usr/local/Cellar/berkeley-db/18.1.40 pip install berkeleydb` – Carl G Oct 03 '21 at 16:10
11

I had a similar issue but none of the suggestions worked for me as I couldn't use AGPL license or a commercial Berkeley license from Oracle.

BERKELEYDB_DIR=$(brew --cellar)/berkeley-db/6.1.26 pip install bsddb3
Collecting bsddb3
Using cached bsddb3-6.1.1.tar.gz
Complete output from command python setup.py egg_info:
Trying to use the Berkeley DB you specified...
Detected Berkeley DB version 6.1 from db.h

******* COMPILATION ABORTED *******

You are linking a Berkeley DB version licensed under AGPL3 or have a commercial license.

AGPL3 is a strong copyleft license and derivative works must be equivalently licensed.

You have two choices:

  1. If your code is AGPL3 or you have a commercial Berkeley DB license from Oracle, please, define the environment variable 'YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION' to any value, and try to install this python library again.

  2. In any other case, you have to link to a previous version of Berkeley DB. Remove Berlekey DB version 6.x and let this python library try to locate an older version of the Berkeley DB library in your system. Alternatively, you can define the environment variable 'BERKELEYDB_DIR', or 'BERKELEYDB_INCDIR' and 'BERKELEYDB_LIBDIR', with the path of the Berkeley DB you want to use and try to install this python library again.

Sorry for the inconvenience. I am trying to protect you.

More details:

    https://forums.oracle.com/message/11184885
    http://lists.debian.org/debian-legal/2013/07/

******* COMPILATION ABORTED *******

However reverting to an older version fixed it.

Install the older version of berkeley-db with brew

brew install berkeley-db4

Then as suggested install bsddb3 with pip

pip install bsddb3

Then

BERKELEYDB_DIR=$(brew --cellar)/berkeley-db4/4.8.30 pip install bsddb3

(modified from Stefan Schmidt's comment to reference the older berkeley-db version directory)

Finally apply patch to dbhash.py as described here.

bamdan
  • 836
  • 7
  • 21
7

@bamdan 's answer uses an older version of Berkeley DB, if you still want to use the latest, Berkeley DB,

  • First, install the latest Berkeley DB

    pip install berkeley-db
    
  • Second, set an environment variable YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION to indicate that you have the license

    BERKELEYDB_DIR=$(brew --cellar)/berkeley-db4/6.1.26 YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=yes pip install bsddb3
    
soulmachine
  • 3,917
  • 4
  • 46
  • 56
  • 1
    Version agnostic version: `BERKELEYDB_DIR=$(brew --prefix berkeley-db) YES_I_HAVE_THE_RIGHT_TO_USE_THIS_BERKELEY_DB_VERSION=yes pip install bsddb3` – Lenar Hoyt Oct 08 '17 at 01:36
  • 2
    I think you meant to type `brew install berkeley-db`. Also, what is the deal with this license? How do I know I have this right? – gosuto Apr 23 '18 at 18:39
0

I just wanted to add something that is currently missing, I'm a Linux Ubuntu/Debian user, but I have to make my python scripts work on macOS systems. I encountered the same issue that user1611830 encountered.

I followed the steps explained by Francisco Roque and Sean above. However, I was obtaining the same initial problem. I saw Bamdan's answer and wanted to see the patch he referred to modify dbhash.py using the link he provided source. But the 'patch' link is no longer working.

I found out that in order to modify dbhash.py as explained in source, one needs to disable the System Integrity Protection, so followed the instructions of this answer disablingSIP. Afterward, I could modify dbhash.py and the new bsddb3 worked and I could use shelve to read in workspaces saved previously.

wariasr
  • 41
  • 2