10

I would like to use the dbm module on my Windows machine, but it is currently only supported on Unix. http://docs.python.org/library/dbm.html

Does anyone know of a similar module with similar syntax or a workaround to get dmb functional on windows? Being able to access a database written to the hard drive much like how I code to access a dictionary would be great. Thank you for your help!

Brian
  • 4,023
  • 8
  • 29
  • 36

4 Answers4

5

Actually, after more googling around, I found this:

http://docs.python.org/library/anydbm.html#module-anydbm

I've tried this on windows and it seems to be working fine =)

Brian
  • 4,023
  • 8
  • 29
  • 36
  • 1
    If you're benchmarking with Python 2 on Windows, you're in for a treat when you switch to Python 3, as dbm falls back to [dumbdbm](https://docs.python.org/3/library/dbm.html#module-dbm.dumb), which is Python's own self-proclaimedly lame dbm implementation, whereas on Python 2 it supports the much faster [Berkeley DB](https://docs.python.org/2/library/dbhash.html). To avoid dumbdbm on Py3/Win, I've used both [LMDB](https://github.com/dw/py-lmdb) and [semidbm](https://github.com/jamesls/semidbm), and ended up writing my own workaround, [Petite](https://github.com/h5rdly/Petite-DB) – Jay Jul 15 '17 at 13:05
  • https://docs.python.org/3/library/anydbm.html: 404 Not Found. Did they remove it? How to use gnu dbm on windows? – Basj Nov 19 '20 at 11:03
  • Try this: https://docs.python.org/3/library/dbm.html – yoonghm Nov 26 '20 at 11:07
  • ["The anydbm module has been renamed to dbm in Python 3"](https://web.archive.org/web/20121025045609/http://docs.python.org/library/anydbm.html) – facehugger Nov 30 '21 at 04:54
3

If Python 3 is of relevance, I'd go for an external k-v solution, as dumbdbm is no joy.

Some pure Python options:

  • semidbm - A faster alternative to dumbdbm, Python standard library only, pip and go. The one I'd go for if I want to ensure portability and availability to users.

  • PickleDB - Uses json to serialize data. Standrad library only, I haven't benchmarked but I suspect it's slower than semidbm because of the serialization overhead.

  • Petite DB - My own simple workaround using Python's zipfile module. Basic testing in the books but it's not production ready.

There are also Python wrappers to LMDB, UnQLite and SQLite4 LSM, all of which support Windows, though the SQLite4 bindings weren't tested.

The latter two are by Charles Leifer, who is both savvy with k-v stores and an avid Python developer (see Peewee).

As far as LMDB, I've tried it for a while. No complaints, but it uses a transactional model, where you can't use it dictionary-style like with other dbm's, unless you subclass/compose/submit a pull request etc. Also, it explicitly doesn't utilize compression (see also) which was something I was interested in.

So LMDB just didn't quite fit my specific needs. It does seem to be highly capable, the bindings worked fine, and installing them was untroublesome (pip worked for me, had no need to install LMDB seperately or any nuisance to that effect).

Jay
  • 2,535
  • 3
  • 32
  • 44
3

Based on the following test on a Windows 7 system using Python 2.7.2 it appears that dbhash is supported on Windows instalations.

import os

import anydbm

import whichdb

file = os.curdir + '/testdbm'   # define a test file name in the current directory

d = anydbm.open(file, 'c')      # create a new database using the test file name

db_type = whichdb.whichdb(file) # get the dbm database type

print(db_type)                  # display the result

'dbhash'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Steve Wood
  • 39
  • 3
2

I think anydbm on Windows will only load dumbdbm, since all the other modules appear to be Unix only. According to the Python documentation...

"The dumbdbm module is intended as a last resort fallback for the anydbm module when no more robust module is available. The dumbdbm module is not written for speed and is not nearly as heavily used as the other database modules."

MarioVilas
  • 912
  • 10
  • 16