12

I use Python 3.6 in an anaconda environment. I installed GDBM with

conda install gdbm

The installation went well, however I can't use dbm.gnu from Python:

ModuleNotFoundError: No module named '_gdbm'

It seams that Python doesn't include the _gdbm module, even if GDBM is actually installed.

Is this a known problem? How can I fix it?

Thanks!

Picani
  • 169
  • 1
  • 8

3 Answers3

18

I faced this issue as well. This is probably not the ideal way, but it works. I did the following to resolve this -

sudo apt-get install python3-gdbm

This installs the gdbm library for python3, however since apt-get and anaconda are two independent package managers; this isn't going to solve your problem. We primarily do this to get a hold of the .so shared library which we will place in the right folder in our anaconda installation. Next we find the location of the .so file using -

dpkg -L python3-gdbm

This gives us the following output -

/.
/usr
/usr/lib
/usr/lib/python3.5
/usr/lib/python3.5/lib-dynload
/usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so
/usr/share
/usr/share/doc
/usr/share/doc/python3-gdbm
/usr/share/doc/python3-gdbm/copyright
/usr/share/doc/python3-gdbm/changelog.Debian.gz
/usr/share/doc/python3-gdbm/README.Debian

The file we require is here -

/usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so

Copy this file to the lib-dynload folder of your anaconda installation; for me this was -

cp /usr/lib/python3.5/lib-dynload/_gdbm.cpython-35m-x86_64-linux-gnu.so /home/username/anaconda3/lib/python3.5/lib-dynload

Note, that this will only work if the directory the .so was copied to is in python's sys.path. To find the correct directory to copy to, assuming you're inside the activated conda environment, run:

python -c 'import sys; [print(x) for x in sys.path if "lib-dynload" in x]'

For example, in my case, the directory was inside the environment path and not in the anaconda main library. ~/anaconda3/envs/myenvname/lib/python3.7/lib-dynload

Now try importing the module in python -

from _gdbm import *

or testing it from the command line:

python -m dbm.gnu

This should have fixed your problem.

Please note, mine is an Ubuntu-16.06 OS and my python version is 3.5.2. The .so file may work with python3.6 as well, if not you can try installing python3.6-gdbm, although a quick search for ubuntu 16.04 didn't give me any results.

stason
  • 5,409
  • 4
  • 34
  • 48
Savvy
  • 547
  • 5
  • 12
  • Thanks for you response. I did not success to make it works before your answer so I wrote [an alternative based on SQLite](https://github.com/Picani/SQLiteKV) which have very good perf in my use-cases. I'll edit this comment if I have time to test your solution. – Picani Apr 03 '18 at 08:51
  • 1
    I used the 3.5 version for conda 3.7 and it worked fine by renaming the file. Thanks. You might want to set permissions like the other files in the directory. – colin Oct 23 '18 at 16:08
  • 3
    Thank you for this useful hack, @Savvy. I edited your answer to add a programmatic way to find the destination directory, since in my case it was under the environment lib path and not in the main anaconda lib path. – stason Dec 30 '18 at 02:16
  • I had to copy the library to `~/miniconda3/lib/python3.7/lib-dynload/` but then it worked at once. Thanks! – Alfe Feb 06 '20 at 16:29
  • I did the same as colin, changing the file name from _gdbm.cpython-39-x86_64-linux-gnu.so to _gdbm.cpython-310-x86_64-linux-gnu.so and setting permissions to rwxrwxr-x using chmod 775 _gdbm.cpython-310-x86_64-linux-gnu.so – Mat Apr 01 '22 at 20:47
0

Although the question is for Python3, I got here while trying to install gdbm on Python2 so I post my answer as it may be useful for others. The correct command was conda install python-gdbm. Although conda install gdbm went through, the module couldn't be imported. As per here, however, this may not work for Python3.

Botond
  • 2,640
  • 6
  • 28
  • 44
0

The answer from @stason worked for me with a little modification regarding the destination route for the .so file. I copied the files to the lib-dynload folder in the environment to make this work.

Instead of: /home/username/anaconda3/lib/python3.X/lib-dynload

I used: /home/username/anaconda3/envs/**your_env**/lib/python3.X/lib-dynload

Doing the same but pointing to this folder worked for me. I hope this helps anyone with the issue.

Thanks!