lmdb (py-lmdb) is a universal Python binding for the LMDB 'Lightning' Database, a fast, memory-efficient, embedded key-value store. It provides an ordered map interface, multi-version concurrency control (MVCC) with reader/writer transactions, and utilizes memory-mapped files for zero-copy operations. The library is actively maintained, with frequent releases; the current version is 2.2.0.
pip install lmdbVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to open an LMDB environment, write key-value pairs, read specific values, iterate through all entries using a cursor, and properly close the environment. Note the use of byte strings for keys and values, and the importance of `map_size` and `max_dbs` configuration. The example also includes basic cleanup.
Always `.encode()` Python strings to bytes before `put()` and `.decode()` bytes to strings after `get()` or cursor iteration.
Estimate your maximum database size and set `map_size` generously when initially opening the environment. Ensure `map_size` is consistent if multiple processes access the same database. If resizing is necessary, ensure only one process is active.
Determine the maximum number of named databases you will use and specify `max_dbs` in `lmdb.open()` when the environment is created or first accessed.
Always use `with env.begin(write=True) as txn:` for write operations. The context manager handles `commit()` on success and `abort()` on exceptions automatically.
Ensure your project runs on Python 3.9 or a newer compatible version. For older Python environments, use `py-lmdb < 1.4.1` (e.g., `pip install 'lmdb<1.4.1'`).
Increase the `map_size` parameter when opening the LMDB environment to a sufficiently large value (e.g., `lmdb.open(path, map_size=1_000_000_000)` for 1GB), or use `env.set_mapsize()` to dynamically expand it.
Install the package using pip: `pip install lmdb`. Ensure you are running this in the correct Python environment if using virtual environments.
Explicitly encode Unicode strings to byte strings (e.g., UTF-8) before passing them to LMDB operations: `key.encode('utf-8')` or `value.encode('utf-8')`.Ensure that `lmdb.open()` is called only once per environment per process. If you need to re-access it, use the existing environment object. If multiprocessing, open a fresh `Environment` in each child process or use `multiprocessing.get_context('spawn')`.No dependency data recorded yet.