Search code examples
pythonmoto

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


import pytest
from moto import mock_s3


@pytest.fixture(scope='module')
def s3():
    with mock_s3():
        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

This code was working, but is now throwing an exception Cannot import name mock_s3 from moto. What am I doing wrong?


Solution

  • Simply replace your import of mock_s3 with from moto import mock_aws and use with mock_aws(): instead.

    Moto was recently bumped to version 5.0, and you were probably running 4.x before.

    https://github.com/getmoto/moto/blob/master/CHANGELOG.md

    If you check the change log, you will see that an important breaking change was made:

    All decorators have been replaced with a single decorator: mock_aws