Registry / type-stubs / moto
library5.2.3pypypi✓ verified 25d ago

Moto is a Python library that allows developers to easily mock AWS services for testing purposes. It intercepts Boto3 calls and routes them to an in-memory mock, simulating AWS API behavior locally without actual AWS interaction. The current version is 5.1.22, and it typically sees new releases every 1-2 weeks, ensuring continuous updates for AWS service support.

pip install moto[all]
INSTALL
IMPORT
SIG · MOTO
M
moto
type-stubspythonv5.2.3
Install
15.9s avg
Import
1691ms
Disk
283MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v5.2.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 1.801s · 256.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 15.9s · import 1.581s · 257MB
283MB installed
● package 283MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

mock_aws
from moto import mock_aws
from moto import mock_s3
As of Moto v5.x, service-specific decorators like `mock_s3` or `mock_dynamodb` have been replaced by the single, unified `mock_aws` decorator.

This quickstart demonstrates how to mock an S3 service using the `@mock_aws` decorator. It creates a Boto3 S3 client within the mocked context, creates a bucket, and then verifies its existence, all without interacting with actual AWS infrastructure. Dummy AWS credentials are set to prevent botocore from attempting real AWS calls.

import boto3 from moto import mock_aws import os @mock_aws def test_s3_bucket_creation(): # Ensure dummy AWS credentials are set for botocore to not attempt real calls os.environ['AWS_ACCESS_KEY_ID'] = 'testing' os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing' os.environ['AWS_SECURITY_TOKEN'] = 'testing' os.environ['AWS_SESSION_TOKEN'] = 'testing' os.environ['AWS_DEFAULT_REGION'] = 'us-east-1' s3_client = boto3.client('s3', region_name='us-east-1') bucket_name = 'my-test-bucket-123' # Create a bucket in the mocked AWS environment s3_client.create_bucket(Bucket=bucket_name) # List buckets and verify the new bucket exists response = s3_client.list_buckets() buckets = [b['Name'] for b in response['Buckets']] assert bucket_name in buckets print(f"Successfully created and verified bucket: {bucket_name}") if __name__ == '__main__': test_s3_bucket_creation()
moto --version
Debug
Known issues
breakingMoto v5.x introduced a significant breaking change: all individual service decorators (e.g., `@mock_s3`, `@mock_dynamodb`, `@mock_sqs`) have been removed and replaced by a single, unified `@mock_aws` decorator.
fix
Replace specific decorators like `@mock_s3` with `@mock_aws`. If you need to specify specific services for the mock, pass them as arguments to `@mock_aws` (though often not necessary for basic usage, it mocks all by default). For example: `@mock_aws(config={'s3': {'use_docker': False}})`.
affects: >= 5.0.0
gotchaBoto3 clients/resources must be created *after* the `moto` mock has been established. If a Boto3 client is initialized globally (e.g., at the module level) before the `@mock_aws` decorator or context manager is activated, `moto` will not intercept those calls, and they may hit real AWS.
fix
Create `boto3` clients/resources inside the `@mock_aws` decorated function, within the `with mock_aws():` block, or within a `setUp` method of a `unittest.TestCase` where the class is decorated. For Pytest, ensure fixtures that create clients are decorated or called within the mock's scope. Using local imports for modules that create `boto3` clients can also help.
affects: All versions
gotchaAlways specify `region_name` explicitly when creating `boto3` clients and resources (e.g., `boto3.client('s3', region_name='us-east-1')`). Moto can sometimes exhibit inconsistent behavior or default to unexpected regions if `region_name` is omitted, potentially leading to hard-to-debug test failures.
fix
Include `region_name='your-desired-region'` in all `boto3.client()` and `boto3.resource()` calls within your mocked tests.
affects: All versions
breakingIn Moto v3.x, the behavior of class decorators changed: the mock state is now reset before *every* test method within a decorated class. Previously, the state was global and shared across methods in the same class.
fix
If your tests relied on shared state between methods in a class, refactor them to ensure each test method sets up its required state independently, or use alternative mechanisms (e.g., pytest fixtures with appropriate scope) to manage state if shared setup is truly necessary.
affects: >= 3.0.0
gotchaFor Pytest users, ensure that any `boto3` client-creating fixtures are correctly integrated with Moto's mocking. If a fixture creates a client before `mock_aws` is active, it won't be mocked.
fix
Decorate your pytest fixtures with `@mock_aws` if they provision AWS clients, or use the `mock_aws` context manager within the fixture's setup phase. Example: `@pytest.fixture @mock_aws def s3_client(): return boto3.client('s3', region_name='us-east-1')`.
affects: All versions
gotchaMoto intercepts HTTP requests and requires dummy AWS credentials to be set (either via environment variables or `~/.aws/credentials`). Without them, `botocore` might attempt to resolve actual credentials or hit real AWS endpoints, leading to errors or unintended side effects.
fix
Before running tests, set environment variables like `AWS_ACCESS_KEY_ID=testing`, `AWS_SECRET_ACCESS_KEY=testing`, `AWS_SECURITY_TOKEN=testing`, `AWS_SESSION_TOKEN=testing`, and `AWS_DEFAULT_REGION=us-east-1`. These can be exported in your test runner or set programmatically.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'moto'
The 'moto' library is not installed in the Python environment where the code is being run.
fix
Install the moto library using pip. If you need specific AWS service mocks, install them with extras (e.g., `[all]` or `[s3,dynamodb]`).
```bash
pip install moto[all]
# Or for specific services:
pip install moto[s3,dynamodb]
```
ImportError: cannot import name 'mock_s3' from 'moto'
This error typically occurs when upgrading Moto to version 5.0 or later, as the individual service decorators (like `mock_s3`, `mock_dynamodb2`) have been replaced by a single `@mock_aws` decorator.
fix
Replace individual service decorators with the unified `@mock_aws` decorator.
```python
from moto import mock_aws

@mock_aws
def test_my_aws_function():
    # Your test code here
    pass
```
botocore.exceptions.NoCredentialsError: Unable to locate credentials
This error happens when `boto3` attempts to make an AWS call but cannot find valid credentials, often because the `moto` mock was not correctly activated before the `boto3` client/resource was initialized, or no dummy credentials were provided.
fix
Ensure that the `moto` decorator (`@mock_aws`) or context manager (`with mock_aws():`) is applied *before* any `boto3` clients or resources are created in your test. Also, it's good practice to set dummy AWS credentials for tests.
```python
import os
import boto3
from moto import mock_aws

# Set dummy credentials (optional, but good practice)
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'

@mock_aws
def test_s3_operation():
    s3_client = boto3.client('s3', region_name='us-east-1')
    # Your S3 operations
    s3_client.create_bucket(Bucket='my-test-bucket')
```
botocore.errorfactory.NoSuchBucket: An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist.
Within a `moto` mock, AWS resources like S3 buckets or DynamoDB tables do not automatically exist; they must be explicitly created in the mock environment before your code attempts to access them.
fix
Create the necessary AWS resource (e.g., S3 bucket, DynamoDB table) within your `moto` decorated test function or fixture before making calls to it.
```python
import boto3
from moto import mock_aws

@mock_aws
def test_list_s3_buckets():
    s3_client = boto3.client('s3', region_name='us-east-1')
    s3_client.create_bucket(Bucket='my-test-bucket') # Create the bucket in the mock environment
    response = s3_client.list_buckets()
    assert 'my-test-bucket' in [b['Name'] for b in response['Buckets']]
```
Upgrade
Version history
5.2.3latest on PyPI · released Aug 22, 2026
Audit
Dependencies
boto3requiredMoto intercepts calls made by boto3, the AWS SDK for Python.
pythonrequiredRequires Python 3.9 or higher.
moto[service_name]optionalVarious service-specific dependencies are installed via extras (e.g., moto[s3], moto[ec2]) or moto[all].
Agent activity
35 hits · last 30 days
node
28
OpenAI (training)
1
Resources