{"id":3216,"library":"plyvel","title":"Plyvel, a fast and feature-rich Python interface to LevelDB","description":"Plyvel is a fast and feature-rich Python interface to LevelDB. It provides a comprehensive, high-performance Pythonic API that wraps most of the underlying LevelDB C++ API, leveraging Cython for speed. Key features include support for write batches, database snapshots, flexible iterators, and prefixed databases. The library is actively maintained, with version 1.5.1 released in January 2024, consistently adding support for new Python versions and ensuring compatibility with recent LevelDB releases.","status":"active","version":"1.5.1","language":"en","source_language":"en","source_url":"https://github.com/wbolster/plyvel","tags":["database","leveldb","key-value store","nosql","embedded database"],"install":[{"cmd":"pip install plyvel","lang":"bash","label":"Recommended Installation"},{"cmd":"sudo apt-get install libleveldb1v5 libleveldb-dev python3-dev\npip install plyvel","lang":"bash","label":"Installation from source (Debian/Ubuntu example)"}],"dependencies":[{"reason":"Core database engine. Pre-built wheels often embed it; source builds require development headers and shared library.","package":"leveldb","optional":false},{"reason":"Required for building from source.","package":"cython","optional":true}],"imports":[{"symbol":"plyvel","correct":"import plyvel"},{"note":"Main class for interacting with a LevelDB database.","symbol":"DB","correct":"db = plyvel.DB(...)"}],"quickstart":{"code":"import plyvel\nimport os\nimport shutil\n\n# Define a path for the LevelDB database\ndb_path = '/tmp/testdb_plyvel_quickstart'\n\n# Clean up any previous database at the path\nif os.path.exists(db_path):\n    shutil.rmtree(db_path)\n\ntry:\n    # Open a new database, creating it if it doesn't exist\n    # Using a context manager ensures the database is properly closed\n    with plyvel.DB(db_path, create_if_missing=True) as db:\n        print(f\"Database opened at: {db_path}\")\n\n        # Put key-value pairs (keys and values must be bytes)\n        db.put(b'name', b'Alice')\n        db.put(b'age', b'30')\n        db.put(b'city', b'New York')\n\n        # Get a value\n        name = db.get(b'name')\n        if name:\n            print(f\"Name: {name.decode('utf-8')}\")\n\n        # Iterate over all key-value pairs\n        print(\"\\nAll entries:\")\n        for key, value in db:\n            print(f\"  {key.decode('utf-8')}: {value.decode('utf-8')}\")\n\n        # Delete a key\n        db.delete(b'age')\n        print(\"\\nAfter deleting 'age':\")\n\n        # Verify deletion by iterating again\n        for key, value in db:\n            print(f\"  {key.decode('utf-8')}: {value.decode('utf-8')}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    # Ensure cleanup even if an error occurs\n    if os.path.exists(db_path):\n        shutil.rmtree(db_path)\n        print(f\"\\nCleaned up database directory: {db_path}\")","lang":"python","description":"This quickstart demonstrates opening a LevelDB database, performing basic put, get, and delete operations, and iterating over stored key-value pairs using `plyvel`. It highlights the recommended practice of using `plyvel.DB` as a context manager for automatic resource management and proper database closing."},"warnings":[{"fix":"Upgrade to Python 3 or pin Plyvel version to <1.3.0 if Python 2 compatibility is essential.","message":"Plyvel 1.3.0 (released October 2020) completely dropped support for Python 2. Users migrating to newer Plyvel versions must ensure their projects are running on Python 3.","severity":"breaking","affected_versions":"<1.3.0 (for Python 2 support)"},{"fix":"Use pre-built binary wheels where available, or install `LevelDB` development headers (e.g., `sudo apt-get install libleveldb-dev`) before `pip install plyvel`.","message":"Installing Plyvel from source (not via pre-built wheels) requires LevelDB development headers and a compatible LevelDB shared library (>= 1.21 for Plyvel 1.4.0+). Failing to provide these can lead to compilation errors or `ImportError` due to undefined symbols. On Linux, `pip install plyvel` often works due to embedded LevelDB in wheels, but if building from source, install `libleveldb-dev` (or equivalent) first.","severity":"gotcha","affected_versions":"All versions when building from source"},{"fix":"Always ensure exclusive access or use `plyvel.DB` as a context manager (`with plyvel.DB(...) as db:`) which handles closing safely at the end of the block.","message":"Closing a `plyvel.DB` instance while it is actively being accessed by other threads can lead to hard crashes due to a lack of internal synchronization. Ensure that no other threads are performing database operations concurrently when calling `DB.close()`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid Python-based custom comparators in performance-critical sections. Rely on LevelDB's default byte-wise comparator or consider alternative key encoding strategies if custom ordering is needed without the performance cost.","message":"Implementing custom LevelDB comparators using Python callables in Plyvel incurs a significant performance penalty (e.g., up to a 4x slowdown for bulk writes) compared to LevelDB's native C++ comparators.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}