{"id":2684,"library":"pydynamodb","title":"PyDynamoDB","description":"PyDynamoDB is a Python DB API 2.0 (PEP 249) client for Amazon DynamoDB, enabling interaction with DynamoDB using SQL-like syntax. It supports both DML operations via PartiQL and DDL operations through MySQL-like statements, and also provides a SQLAlchemy dialect. The current version is 0.8.2. The library has a consistent release cadence, with several updates in recent months, indicating active maintenance. [1, 6]","status":"active","version":"0.8.2","language":"en","source_language":"en","source_url":"https://github.com/passren/PyDynamoDB","tags":["aws","dynamodb","database","sql-like","db-api","partiql"],"install":[{"cmd":"pip install pydynamodb","lang":"bash","label":"Install PyDynamoDB"}],"dependencies":[{"reason":"AWS SDK for Python, required for interacting with DynamoDB. Specific versions boto3 >= 1.21.0 and botocore >= 1.24.7 are required.","package":"boto3","optional":false},{"reason":"Retry utility for API calls.","package":"tenacity","optional":false},{"reason":"Only required if using the PyDynamoDB SQLAlchemy Dialect.","package":"SQLAlchemy","optional":true},{"reason":"Required for parsing SQL-like grammars, particularly if using DDL or advanced queries.","package":"pyparsing","optional":true}],"imports":[{"note":"PyDynamoDB is distinct from PynamoDB (an ORM library) despite similar names. Ensure you import from 'pydynamodb'.","wrong":"from pynamodb import connect","symbol":"connect","correct":"from pydynamodb import connect"}],"quickstart":{"code":"import os\nfrom pydynamodb import connect\n\naws_access_key_id = os.environ.get('AWS_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY')\naws_secret_access_key = os.environ.get('AWS_SECRET_ACCESS_KEY', 'YOUR_SECRET_KEY')\nregion_name = os.environ.get('AWS_DEFAULT_REGION', 'us-east-1')\n\ntry:\n    # Establish a connection\n    conn = connect(\n        aws_access_key_id=aws_access_key_id,\n        aws_secret_access_key=aws_secret_access_key,\n        region_name=region_name\n    )\n\n    # Create a cursor object\n    cursor = conn.cursor()\n\n    # Execute a DDL statement (MySQL-like syntax)\n    cursor.execute(\"CREATE TABLE IF NOT EXISTS MyTestTable (id STRING HASH KEY, name STRING)\")\n    print(\"Table 'MyTestTable' created or already exists.\")\n\n    # Execute a DML statement (PartiQL syntax)\n    cursor.execute(\"INSERT INTO MyTestTable VALUE {'id': ?, 'name': ?}\", '1', 'Alice')\n    print(\"Inserted item 'Alice'.\")\n\n    # Query data\n    cursor.execute(\"SELECT * FROM MyTestTable WHERE id = ?\", '1')\n    result = cursor.fetchall()\n    print(f\"Queried result: {result}\")\n\n    # Clean up (optional)\n    # cursor.execute(\"DROP TABLE MyTestTable\")\n    # print(\"Table 'MyTestTable' dropped.\")\n\n    # Close the connection\n    cursor.close()\n    conn.close()\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to connect to DynamoDB, create a table using DDL, insert data using PartiQL, and query it. Ensure you have AWS credentials configured (e.g., via environment variables `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_DEFAULT_REGION`) or replace the placeholders. [6]"},"warnings":[{"fix":"Verify your imports (`from pydynamodb import connect` for PyDynamoDB) and consult the correct documentation for the chosen library.","message":"PyDynamoDB is a DB API 2.0 client, not an ORM like PynamoDB. They are distinct libraries with different APIs and features. Ensure you are using the correct library and its corresponding import paths.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Design your DynamoDB table schema with access patterns in mind, focusing on denormalization, efficient primary key design (hash and range keys), and utilizing indexes for filtering. Avoid using filter expressions for primary filtering.","message":"DynamoDB is a NoSQL database and differs significantly from relational databases. Common SQL patterns like normalizing data, simple primary key designs, and over-reliance on filter expressions lead to inefficient and costly operations. Filter expressions are applied *after* scan/query, impacting performance. [19, 22]","severity":"gotcha","affected_versions":"All versions"},{"fix":"Convert Python float values to `Decimal` objects (from the `decimal` module) before writing them to DynamoDB to ensure accurate representation and storage. For example, `Decimal(str(my_float_value))`.","message":"DynamoDB does not natively support float types for numeric values; it expects decimal types. Passing standard Python floats can lead to unexpected behavior or data loss due to precision issues when interacting with the service. [20]","severity":"gotcha","affected_versions":"All versions"},{"fix":"Reserve transactions for cases where two or more items absolutely must succeed or fail together. For other scenarios, evaluate if conditional expressions are sufficient. Model data to minimize the need for multi-item transactions.","message":"Overusing DynamoDB transactions can lead to higher costs and performance issues, as each transactional write consumes twice the write capacity. Transactions are limited to 25 items and 4MB total size. Crossing updates on items in parallel transactions can also cause `TransactionCanceledException`. [21]","severity":"gotcha","affected_versions":"All versions"},{"fix":"Implement robust error handling around your DynamoDB interactions to manage various service-specific exceptions and ensure application stability.","message":"Always wrap DynamoDB operations with try-catch blocks to gracefully handle failures, even though AWS SDKs (like boto3, which PyDynamoDB uses) handle internal retries with exponential backoff. Specific DynamoDB errors (e.g., `ConditionalCheckFailedException`, `ValidationException`, `ResourceNotFoundException`) require explicit handling. [20]","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}