boto / boto3

AWS SDK for Python
https://aws.amazon.com/sdk-for-python/
Apache License 2.0
9.07k stars 1.87k forks source link

Import failure on `from boto3.s3.transfer import TransferConfig` #3951

Closed PSRCode closed 11 months ago

PSRCode commented 11 months ago

Describe the bug

Hi,

Since Boto3 v1.33.0 we see a failure to import the following from our code application:

from boto3.s3.transfer import TransferConfig

Resulting in the following redacted Traceback:

/usr/local/lib/python3.10/dist-packages/_pytest/python.py:617: in _importtestmodule
    mod = import_path(self.path, mode=importmode, root=self.config.rootpath)
/usr/local/lib/python3.10/dist-packages/_pytest/pathlib.py:567: in import_path
    importlib.import_module(module_name)
/usr/lib/python3.10/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1050: in _gcd_import
    ???
<frozen importlib._bootstrap>:1027: in _find_and_load
    ???
<frozen importlib._bootstrap>:1006: in _find_and_load_unlocked
    ???
<frozen importlib._bootstrap>:688: in _load_unlocked
    ???
/usr/local/lib/python3.10/dist-packages/_pytest/assertion/rewrite.py:178: in exec_module
    exec(co, module.__dict__)
src/tests/test_s3bucket.py:2: in <module>
    from XXX.utils import s3_obj_helper
src/XXX/utils/s3_obj_helper.py:7: in <module>
    from boto3.s3.transfer import TransferConfig
/home/psrcode/.local/lib/python3.10/site-packages/boto3/s3/transfer.py:146: in <module>
    from boto3.crt import create_crt_transfer_manager
/home/psrcode/.local/lib/python3.10/site-packages/boto3/crt.py:26: in <module>
    from s3transfer.crt import (
/home/psrcode/.local/lib/python3.10/site-packages/s3transfer/crt.py:28: in <module>
    from awscrt.s3 import (
E   ImportError: cannot import name 'S3ResponseError' from 'awscrt.s3' (/usr/local/lib/python3.10/dist-packages/awscrt/s3.py)

The same import is working fine on 1.29.7.

Looking at the change log not much seems to be related apart from this: * feature:``s3``: Boto3 will now opt into using the awscrt on select EC2 instance types for s3 transfers.

For now we will freeze our version locally to 1.29.7.

Cheers

Expected Behavior

The import should work.

Current Behavior

The import is broken.

Reproduction Steps

from boto3.s3.transfer import TransferConfig

Possible Solution

No response

Additional Information/Context

No response

SDK version used

1.33.0/1

Environment details (OS name and version, etc.)

Ubuntu 22.04.2

nateprewitt commented 11 months ago

Hi @PSRCode,

Thanks for reaching out about this. It looks like you may have an older version of the awscrt installed alongside Boto3.

With the release of Boto3 1.33.0, it requires awscrt==0.19.17 for certain functionality. The version of the awscrt you're using doesn't have these available, so the quickest fix is to upgrade the awscrt.

If that's not an option, you can disable boto3 using the awscrt with the BOTO_DISABLE_CRT environment variable. Let me know if these options aren't sufficient and we can talk more specifics.

PSRCode commented 11 months ago

Hi @nateprewitt,

Thanks for the quick response. Ok, we will make sure to use awscrt > 0.19.17. I'll report back when I have some time to look into it. What's weird is that we do not define a version for "awscrt" in our requirement.txt so I would expect that we use latest.

Maybe a noob question, how would one should have "known" that we need that version. I would have expected boto3 to "enforce" the version for awscrt.

nateprewitt commented 11 months ago

The awscrt is an optional dependency that Boto3 will use to optimize functionality if it's present. It's not (and currently can't be) a required dependency for Boto3. It should be kept up to date across AWS products that use it. The main other culprits would be the AWSCLI v2 and the AWS IoT SDK for Python v2.

As far as knowing, this feature has been in place for ~2 years and is intended to be a no-hassle opt-in. That does however make it harder to track for dependency management. If you know you're installing boto3 in conjunction with one of the other tools above, you can change your requirement to boto3[crt] which will ensure you always have the minimum required awscrt version.

We're discussing options to try to minimize (or ideally eliminate) these kinds of import issues with version mismatches and the awscrt going forward.

lisenet commented 11 months ago

We've hit this very issue today on Amazon Linux 2023 that comes with python3-awscrt-0.16.7-1.amzn2023.0.1.

We can't easily upgrade awscrt as it's part of Beanstalk AMI, so the only workaround for now is to freeze our version of boto3 to 1.29.7.

nateprewitt commented 11 months ago

Thanks for letting us know @lisenet. We found the CRT copy on AL2023 was outdated yesterday and are working on a fix to help mitigate that case. We'll provide an update here once that's available.

davidrobertson commented 11 months ago

Same @nateprewitt - the updated crt version is not yet available in the AWS repos. I'm willing to bet this broke a lot of things for a lot of organizations. We had to pin the older version of boto3 as well.

lisenet commented 11 months ago

My guess is that since boto3, awscrt and AL2023 are all Amazon software, there was no integration test that would include all three of them together. Otherwise this would've been picked up before releasing the code.

nateprewitt commented 11 months ago

That's correct, AL2023 adding the CRT to the default Python setup was not a known integration point before yesterday. A quick note of clarity that boto3 should always be used in a virtualenv to avoid unintentional interactions with the default Python installation, but we're in the process of releasing a change to fix this now.

We expect this issue to be resolved in 1.33.3.

lisenet commented 11 months ago

Superb, appreciate the response.

nateprewitt commented 11 months ago

Alright, 1.33.3 is published. I'll leave this open for a bit to gather any feedback/issues but will plan to close this tomorrow if we don't hear back. Thanks everyone for your patience.

hsec commented 11 months ago

Evening. I've also been having an issue. I just updated and I'm still receiving the following error:

ImportError: cannot import name 'S3ResponseError' from 'awscrt.s3' (/usr/local/lib/python3.12/site-packages/awscrt/s3.py)

Installed is the following:

pip freeze | grep awscrt -------> awscrt==0.19.18

pip freeze | grep boto3. -------> boto3==1.33.3

It's probably me, however all was good last week.

Many thanks for any help you could provide.

nateprewitt commented 11 months ago

Hi @hsec, yes this was from a change made yesterday. What version of s3transfer do you have installed? If it's not the latest, 0.8.2 you will still have this issue.

hsec commented 11 months ago

Hi. Thank you for responding. I have 0.8.1. I ran pip install s3transfer>=0.8.2, but no update was available.

nateprewitt commented 11 months ago

0.8.2 should be available on PyPI.

Also to double check from what you wrote above, you're using pip freeze to display versions. Is this error happening locally or on Lambda? S3ResponseError is available in awscrt>=0.19.11 so the version of CRT being used isn't what's displayed in pip freeze.

hsec commented 11 months ago

Hi. This error is local. I successfully installed the 0.8.2 version:

Uninstalling s3transfer-0.8.1: Successfully uninstalled s3transfer-0.8.1 Successfully installed s3transfer-0.8.2

and the previous error has gone away.

I do however now get a S3ResponseError:

ImportError: cannot import name 'S3ResponseError' from 'awscrt.s3' (/usr/local/lib/python3.12/site-packages/awscrt/s3.py)

Perhaps I'm doing something incorrectly.

Do I need to update all of the components to the latest version?

nateprewitt commented 11 months ago

You'll either need everything with the current requirements or the older ones. You won't be able to mix old and new. If you start a fresh virtualenv and pip install boto3[crt] are you still getting the error?

i.e.

python -m venv .venv
source .venv/bin/activate
python -m pip install "boto3[crt]"
python -c 'import awscrt;import boto3;import botocore;import s3transfer;print(f"Boto3: {boto3.__version__}, Botocore: {botocore.__version__}, S3Transfer: {s3transfer.__version__}, CRT: {awscrt.__version__}")'

If the above still fails, you may try these one at a time:

python -c 'import awscrt; print(f"CRT: {awscrt.__version__}")'
python -c 'import boto3; print(f"Boto3: {boto3.__version__}")'
python -c 'import botocore; print(f"Botocore: {botocore.__version__}")'
python -c 'import s3transfer; print(f"Botocore: {botocore.__version__}")'
hsec commented 11 months ago

Unfortunately, I'm still getting the same error.

hsec commented 11 months ago

What version of the modules (in their entirety), still work, and wha would be the besrt way to about that?

Many thanks in advance.

hsec commented 11 months ago

Good morning. I uninstalled boto3 (1.33.3) and installed version 1.33.4 and all is good again. Thank you again for your patience and rapid response.

nateprewitt commented 11 months ago

Great to hear, @hsec. I'm going to put this in auto-resolve since the original issue is no longer reproducible and we haven't heard back on further issues in the last 24 hours.

github-actions[bot] commented 11 months ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.