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]
pip install pydynamodbVerified import paths — ran on the pinned version, not inferred.
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]
Verify your imports (`from pydynamodb import connect` for PyDynamoDB) and consult the correct documentation for the chosen library.
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.
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))`.
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.
Implement robust error handling around your DynamoDB interactions to manage various service-specific exceptions and ensure application stability.
Verify the exact name of your DynamoDB table, ensure your AWS credentials and region configuration are correct, and confirm the table is in an 'ACTIVE' state.
Enclose the table name in double quotes within your PartiQL statements, for example: `SELECT * FROM "my-table-with-dash"`.
Ensure `pydynamodb` and SQLAlchemy are installed (`pip install pydynamodb SQLAlchemy`) and that your SQLAlchemy engine creation uses the correct dialect name, typically `dynamodb://` or `pydynamodb://`.
Ensure that your `WHERE` clause explicitly filters on the hash key of the primary key or the Global Secondary Index you are querying, in addition to any sort key conditions, for example: `SELECT * FROM "my-table"."my-gsi" WHERE "PartitionKeyName" = 'some_value' ORDER BY "SortKeyName"`.