getsentry / sentry-python

The official Python SDK for Sentry.io
https://sentry.io/for/python/
MIT License
1.88k stars 492 forks source link

ImportWarning caused by integrations on Python 3.10 #2774

Closed craigds closed 3 months ago

craigds commented 7 months ago

How do you use Sentry?

Self-hosted/on-premise

Version

1.40.5

Steps to Reproduce

Call sentry_sdk.init() while ImportWarnings are enabled, or convert them to error to see traceback

$ python -W error::ImportWarning
Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import sentry_sdk
>>> sentry_sdk.init()

Expected Result

it doesn't produce any ImportWarnings

Actual Result

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 939, in _find_spec
AttributeError: '_ImportRedirect' object has no attribute 'find_spec'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/venv/lib/python3.10/site-packages/sentry_sdk/hub.py", line 102, in _init
    client = Client(*args, **kwargs)  # type: ignore
  File "/venv/lib/python3.10/site-packages/sentry_sdk/client.py", line 173, in __init__
    self._init_impl()
  File "/venv/lib/python3.10/site-packages/sentry_sdk/client.py", line 285, in _init_impl
    self.integrations = setup_integrations(
  File "/venv/lib/python3.10/site-packages/sentry_sdk/integrations/__init__.py", line 119, in setup_integrations
    for integration_cls in iter_default_integrations(
  File "/venv/lib/python3.10/site-packages/sentry_sdk/integrations/__init__.py", line 46, in iter_default_integrations
    yield getattr(import_module(module), cls)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/venv/lib/python3.10/site-packages/sentry_sdk/integrations/celery.py", line 41, in <module>
    from celery import Task, Celery
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
  File "/venv/lib/python3.10/site-packages/celery/local.py", line 460, in __getattr__
    module = __import__(self._object_origins[name], None, None,
  File "/venv/lib/python3.10/site-packages/celery/app/__init__.py", line 2, in <module>
    from celery import _state
  File "/venv/lib/python3.10/site-packages/celery/_state.py", line 15, in <module>
    from celery.utils.threads import LocalStack
  File "/venv/lib/python3.10/site-packages/celery/utils/__init__.py", line 6, in <module>
    from kombu.utils.objects import cached_property
  File "/venv/lib/python3.10/site-packages/kombu/utils/__init__.py", line 6, in <module>
    from .compat import fileno, maybe_fileno, nested, register_after_fork
  File "/venv/lib/python3.10/site-packages/kombu/utils/compat.py", line 12, in <module>
    from kombu.exceptions import reraise
  File "/venv/lib/python3.10/site-packages/kombu/exceptions.py", line 9, in <module>
    from amqp import ChannelError, ConnectionError, ResourceError
  File "/venv/lib/python3.10/site-packages/amqp/__init__.py", line 31, in <module>
    from .connection import Connection  # noqa
  File "/venv/lib/python3.10/site-packages/amqp/connection.py", line 13, in <module>
    from . import __version__, sasl, spec
  File "/venv/lib/python3.10/site-packages/amqp/sasl.py", line 156, in <module>
    GSSAPI = _get_gssapi_mechanism()
  File "/venv/lib/python3.10/site-packages/amqp/sasl.py", line 81, in _get_gssapi_mechanism
    import gssapi
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1002, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 941, in _find_spec
  File "<frozen importlib._bootstrap>", line 914, in _find_spec_legacy
ImportWarning: _ImportRedirect.find_spec() not found; falling back to find_module()

This seems to happen while processing the celery and falcon integrations. Note: I have celery installed but I don't have falcon installed. It may also happen with other integrations; I haven't tested with all the default integrations.

packages installed

Can't include full pip freeze output, but here's a few that might be relevant

billiard==4.2.0
celery==5.3.6
django-celery-results==2.4.0
importlib-metadata==4.6.4
kombu==5.3.5
sentry-sdk==1.40.5
six==1.16.0

Python 3.10.12, installed via apt Ubuntu 22.04.4 LTS

sentrivana commented 7 months ago

Thanks @craigds, we'll take a look.

antonpirker commented 4 months ago

Hey @craigds !

I have now tried this with Sentry SDK version 2.0.0 and it works now:

(.venv) antonpirker@Y7CYJ0XDQY tmp/deleteme » pip install sentry-sdk==2.0.0
Collecting sentry-sdk==2.0.0
  Downloading sentry_sdk-2.0.0-py2.py3-none-any.whl (266 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 266.8/266.8 kB 1.1 MB/s eta 0:00:00
Collecting urllib3>=1.26.11
  Using cached urllib3-2.2.1-py3-none-any.whl (121 kB)
Collecting certifi
  Using cached certifi-2024.6.2-py3-none-any.whl (164 kB)
Installing collected packages: urllib3, certifi, sentry-sdk
Successfully installed certifi-2024.6.2 sentry-sdk-2.0.0 urllib3-2.2.1

[notice] A new release of pip is available: 23.0.1 -> 24.0
[notice] To update, run: pip install --upgrade pip
(.venv) antonpirker@Y7CYJ0XDQY tmp/deleteme » python -V
Python 3.10.13
(.venv) antonpirker@Y7CYJ0XDQY tmp/deleteme » python -W error::ImportWarning
Python 3.10.13 (main, Jan 22 2024, 14:59:06) [Clang 15.0.0 (clang-1500.1.0.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sentry_sdk
>>> sentry_sdk.init()
<sentry_sdk.hub._InitGuard object at 0x101609780>
>>> 

Can you confirm that it is working for you when using version 2.0.0 or newer?

craigds commented 3 months ago

Thanks, yes I can confirm this is fixed now 👍