dynobase-icon
Dynobase

Error: name key is not defined dynamodb

Rafal Wilinski

Answered by Rafal Wilinski

Last updated: October 23rd, 2026

Solution

NameError: name 'Key' is not defined is a Python name-resolution error. Import the boto3 condition helper before building a resource-level query; changing DynamoDB permissions will not fix it.

import boto3
from boto3.dynamodb.conditions import Key

table = boto3.resource("dynamodb").Table("Employees")
response = table.query(
    KeyConditionExpression=Key("pk").eq("USER#1")
)
print(response.get("Items", []))

Use the case-sensitive class name Key. It builds key conditions for the resource API; Attr is the helper for non-key filter conditions. A low-level boto3 client expects a string KeyConditionExpression and typed ExpressionAttributeValues instead of these condition objects.

If the import exists but the error remains, check whether it is inside a function or conditional branch that did not execute. Ensure pk is actually the partition key of the table or index being queried, and paginate if the response contains LastEvaluatedKey.

See Python query examples and AWS documentation.

Dynobase is a Professional GUI Client for DynamoDB

Start your 7-day free trial today

Product Features

Download
/
Pricing
/
Member Portal
/
Privacy
/
EULA
/
Twitter
© 2026 Dynobase
+
Still using AWS DynamoDB Console?
Try Dynobase to accelerate your DynamoDB workflow. Start your 7-day free trial today.