Error: name key is not defined dynamodb

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.
Other Common DynamoDB Errors (with Solutions)
- dynamodb streams missing events
- localstack dynamodb not working
- dynamodb cannot assign requested address
- DynamoDB throttling error
- DynamoDB ConditionalCheckFailedException
- aws.dynamodb.converter.unmarshall not working
- moto dynamodb not working
- dynamodb cannot read property 's' of undefined
- DynamoDB Duplicate Key Error
- ConfigError: Missing region in config
- DynamoDB scan filter expression not working
- DynamoDB local error unable to access JAR file dynamodblocal.jar
- DynamoDB query is slow
- ImportError: No module named boto3 (DynamoDB)
- dynamodb exclusivestartkey not working
Dynobase is a Professional GUI Client for DynamoDB
Start your 7-day free trial today