-
Notifications
You must be signed in to change notification settings - Fork 555
Description
Using Python 3.7. I have the following libraries installed, among others:
django==2.2.2
djangorestframework==3.9.4
pytest-django==3.5.0
pytest==4.6.3
sentry-sdk==0.9.1
In my settings.py
:
# ...
SECRET_KEY = "some value"
# ...
sentry_sdk.init(
dsn=SENTRY_DSN,
environment=ENVIRONMENT,
release=f"project@{VERSION}",
integrations=[DjangoIntegration()],
)
Starting the application via the builtin test server works fine. When invoking pytest
, I'm getting the following error:
Traceback (most recent call last):
File "/usr/local/bin/pytest", line 10, in <module>
sys.exit(main())
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 63, in main
config = _prepareconfig(args, plugins)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 207, in _prepareconfig
pluginmanager=pluginmanager, args=args
File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 81, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
gen.send(outcome)
File "/usr/local/lib/python3.7/site-packages/_pytest/helpconfig.py", line 94, in pytest_cmdline_parse
config = outcome.get_result()
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
raise ex[1].with_traceback(ex[2])
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
res = hook_impl.function(*args)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 687, in pytest_cmdline_parse
self.parse(args)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 895, in parse
self._preparse(args, addopts=addopts)
File "/usr/local/lib/python3.7/site-packages/_pytest/config/__init__.py", line 851, in _preparse
early_config=self, args=args, parser=self._parser
File "/usr/local/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 87, in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
File "/usr/local/lib/python3.7/site-packages/pluggy/manager.py", line 81, in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
return outcome.get_result()
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
raise ex[1].with_traceback(ex[2])
File "/usr/local/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
res = hook_impl.function(*args)
File "/usr/local/lib/python3.7/site-packages/pytest_django/plugin.py", line 333, in pytest_load_initial_conftests
dj_settings.DATABASES
File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 157, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/app/backend/testing_settings.py", line 1, in <module>
from .settings import * # noqa
File "/app/backend/settings.py", line 155, in <module>
integrations=[DjangoIntegration()]
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/hub.py", line 74, in init
client = Client(*args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/client.py", line 88, in __init__
options["integrations"], with_defaults=options["default_integrations"]
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/__init__.py", line 80, in setup_integrations
type(integration).setup_once()
File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/django/__init__.py", line 128, in setup_once
from rest_framework.views import APIView # type: ignore
File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py", line 19, in <module>
from rest_framework.schemas import DefaultSchema
File "/usr/local/lib/python3.7/site-packages/rest_framework/schemas/__init__.py", line 32, in <module>
authentication_classes=api_settings.DEFAULT_AUTHENTICATION_CLASSES,
File "/usr/local/lib/python3.7/site-packages/rest_framework/settings.py", line 220, in __getattr__
val = self.user_settings[attr]
File "/usr/local/lib/python3.7/site-packages/rest_framework/settings.py", line 211, in user_settings
self._user_settings = getattr(settings, 'REST_FRAMEWORK', {})
File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 79, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 66, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.7/site-packages/django/conf/__init__.py", line 176, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.")
django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
It's basically a kind of a circular import error.
It seems that this is due to the REST framework importing settings while settings are still being imported (but due to the proxy nature of django.conf.settings
, this is slightly hidden).
The issue is triggered from the from rest_framework.views import APIView
line, which was introduced in 0.9.1 (I've tried 0.9.0 and it's fine).
I'm not entirely sure yet, why it only happens with pytest
; it's a complex interaction between specific (latest) versions of several packages, but none of them are obscure.