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!
-
Are you using cpython on Windows?Laurion Burchall– Laurion Burchall2010年08月04日 20:41:58 +00:00Commented Aug 4, 2010 at 20:41
-
GNUdbm and ndbm seem not supported on Windows: stackoverflow.com/questions/64909747/…Basj– Basj2020年11月19日 11:04:52 +00:00Commented Nov 19, 2020 at 11:04
4 Answers 4
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 =)
4 Comments
2025 Update
As of Python 3.13, the standard dbm module uses sqilte as its backend -
This module uses the standard library sqlite3 module to provide an SQLite backend for the dbm module. The files created by dbm.sqlite3 can thus be opened by sqlite3, or any other SQLite browser, including the SQLite CLI.
This presumably means a consistent improved performance across platforms.
Original Answer
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).
Comments
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."
Comments
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'
1 Comment
dbhash was removed in Python 3.