Skip to content

ImportError: cannot import mock_s3 from moto ​

Problem Statement ​

When upgrading Moto (a popular library for mocking AWS services in tests), developers encounter an ImportError when trying to import mock_s3:

python
ImportError: cannot import name 'mock_s3' from 'moto'

This commonly occurs in test fixtures like the following example:

python
import pytest
from moto import mock_s3  # Fails here

@pytest.fixture(scope='module')
def s3():
    with mock_s3():  # Previously succeeded here
        os.environ['AWS_ACCESS_KEY_ID'] = 'test'
        # ... setup code ...
        s3 = boto3.resource('s3')
        s3.create_bucket(Bucket='test_bucket')
        yield s3

Cause: Moto introduced a breaking change in version 5.0 where service-specific mocks (like mock_s3, mock_ec2, etc.) were replaced with a unified decorator.

Solution ​

Primary Fix ​

Replace mock_s3 with the universal mock_aws decorator in both imports and usage:

python
from moto import mock_aws  # ✅ Updated import

@pytest.fixture(scope='module')
def s3():
    with mock_aws():  # ✅ Updated context manager
        os.environ['AWS_ACCESS_KEY_ID'] = 'test'
        os.environ['AWS_SECRET_ACCESS_KEY'] = 'test'
        os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
        s3 = boto3.resource('s3')
        s3.create_bucket(Bucket='test_bucket')
        yield s3

Key changes:

  1. Import mock_aws instead of mock_s3
  2. Use the mock_aws() context manager

Why This Works ​

Moto 5.0 consolidated AWS service mocks into a single entry point:

  • Old Approach: Separate decorators per service (mock_s3, mock_ec2, etc.)
  • New Approach: Unified mock_aws handles all services

Compatibility

This change solves the import error and:

  1. Maintains all original functionality
  2. Works with other AWS services (S3, EC2, Lambda, etc.)
  3. Reduces maintenance overhead for tests

Secondary Solution (If Downgrading Is Required) ​

If you temporarily need to use legacy code, pin Moto to a pre-5.0 version:

bash
pip install "moto<5.0"  # Version 4.x

WARNING

This approach is not recommended for new projects. Moto 4.x will not receive updates or security fixes.

Explanation ​

Background on the Breaking Change ​

Moto 5.0 (release notes) standardized mocking with:

Breaking Change: All decorators replaced with a single decorator: mock_aws

This change:

  • Simplifies API usage
  • Reduces cognitive load for developers
  • Ensures future consistency across AWS services

Best Practices for Migrating ​

  1. Search and Replace:
    Replace all occurrences of:

    • from moto import mock_<service> → from moto import mock_aws
    • @mock_<service>() → @mock_aws()
    • with mock_<service>(): → with mock_aws():
  2. Validate Tests:
    Ensure your tests still:

    • Interact with mock AWS resources correctly
    • Pass with the updated syntax

Conclusion ​

Resolve ImportError: cannot import name 'mock_s3' from 'moto' by updating:

  1. Imports: Replace mock_s3 with mock_aws
  2. Usage: Change context managers/decorators to mock_aws()

Migrating to mock_aws ensures compatibility with Moto 5.0+ and provides a future-proof solution for mocking AWS services in tests.