No module named 'gdbm'

sudo apt-get install python3.5-gdbm

helped me


In my case, if I tried running any command that doesn't exist in a bash shell I would get this gross error:

$ programthatdoesntexist
Traceback (most recent call last):
  File "/usr/lib/python3.5/dbm/gnu.py", line 4, in <module>
    from _gdbm import *
ImportError: No module named '_gdbm'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 7, in <module>
    import dbm.gnu as gdbm
  File "/usr/lib/python3.5/dbm/gnu.py", line 6, in <module>
    raise ImportError(str(msg) + ', please install the python3-gdbm package')
ImportError: No module named '_gdbm', please install the python3-gdbm package

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 27, in <module>
    from CommandNotFound.util import crash_guard
  File "/usr/lib/python3/dist-packages/CommandNotFound/__init__.py", line 3, in <module>
    from CommandNotFound.CommandNotFound import CommandNotFound
  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 9, in <module>
    import gdbm
ImportError: No module named 'gdbm'

None of the answers above worked in my case. Although this problem has been solved, this might help someone. If you are still having this issue, maybe this will help:

I had installed python3.6 from the repository ppa.launchpad.net/jonathonf/python-3.6/ubuntu , and apparently that broke some things.

When tying to reinstall the packages command-not-found python3-commandnotfound python3-gdbm python3-gdbm-dbg sessioninstaller, I noticed that apt was pulling sources from here:

Get:1 http://ppa.launchpad.net/jonathonf/python-3.6/ubuntu xenial/main amd64 python3-gdbm amd64 3.6.5-3~16.04.york0.2 [14.6 kB]

To fix the issue, I simply removed that repository:

sed -i ' s/^/#/' /etc/apt/sources.list.d/jonathonf-ubuntu-python-3_6-xenial.list

And than re-ran this command (as root):

apt update ; apt purge python3-gdbm ; apt install command-not-found python3-commandnotfound python3-gdbm python3-gdbm-dbg sessioninstaller

Now, everything works again:

$ lol
No command 'lol' found, did you mean:
 Command 'sol' from package 'aisleriot' (main)
 Command 'col' from package 'bsdmainutils' (main)
lol: command not found

Have you tried installing the package python3-gdbm

sudo apt-get install python3-gdbm