Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests fail with error "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet" #373

Closed
akashdoppalapudi opened this issue May 18, 2024 · 4 comments

Comments

@akashdoppalapudi
Copy link

akashdoppalapudi commented May 18, 2024

I'm trying to package anymail 10.3 for Debian.

I'm using django version 4.2.13 with anymail.

When I run tests they fail with error "django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet."

Many of the tests fail with different stack traces but end up on this error.

I'll give two of those stack traces

ERROR: test_warns_if_no_auth (tests.test_sparkpost_webhooks.SparkPostWebhookSecurityTestCase.test_warns_if_no_auth)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/django/test/utils.py", line 461, in inner
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/tests/webhook_cases.py", line 109, in test_warns_if_no_auth
    response = self.call_webhook()
               ^^^^^^^^^^^^^^^^^^^
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/tests/test_sparkpost_webhooks.py", line 16, in call_webhook
    return self.client.post(
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/test/client.py", line 948, in post
    response = super().post(
               ^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/test/client.py", line 482, in post
    return self.generic(
           ^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/test/client.py", line 609, in generic
    return self.request(**r)
           ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/test/client.py", line 886, in request
    response = self.handler(environ)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/test/client.py", line 163, in __call__
    self.load_middleware()
  File "/usr/lib/python3/dist-packages/django/core/handlers/base.py", line 40, in load_middleware
    middleware = import_string(middleware_path)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/module_loading.py", line 30, in import_string
    return cached_import(module_path, class_name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/module_loading.py", line 15, in cached_import
    module = import_module(module_path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/usr/lib/python3/dist-packages/django/contrib/auth/middleware.py", line 3, in <module>
    from django.contrib.auth.backends import RemoteUserBackend
  File "/usr/lib/python3/dist-packages/django/contrib/auth/backends.py", line 4, in <module>
    from django.contrib.auth.models import Permission
  File "/usr/lib/python3/dist-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/usr/lib/python3/dist-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/usr/lib/python3/dist-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

The next stack trace contains the above error but ends at translation related error

Traceback (most recent call last):
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/anymail/utils.py", line 593, in force_non_lazy_dict
    return {key: force_non_lazy_dict(value) for key, value in obj.items()}
                                                              ^^^^^^^^^
AttributeError: '__proxy__' object has no attribute 'items'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/django/utils/translation/trans_real.py", line 215, in _add_installed_apps_translations
    app_configs = reversed(apps.get_app_configs())
                           ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 147, in get_app_configs
    self.check_apps_ready()
  File "/usr/lib/python3/dist-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/tests/test_utils.py", line 334, in test_force_dict
    result = force_non_lazy_dict(
             ^^^^^^^^^^^^^^^^^^^^
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/anymail/utils.py", line 593, in force_non_lazy_dict
    return {key: force_non_lazy_dict(value) for key, value in obj.items()}
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/anymail/utils.py", line 593, in <dictcomp>
    return {key: force_non_lazy_dict(value) for key, value in obj.items()}
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/anymail/utils.py", line 595, in force_non_lazy_dict
    return force_non_lazy(obj)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/akash/Packaging/django-anymail/django-anymail/.pybuild/cpython3_3.11_anymail/build/anymail/utils.py", line 577, in force_non_lazy
    return str(obj)
           ^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/functional.py", line 155, in __text_cast
    return func(*self.__args, **self.__kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/translation/__init__.py", line 95, in gettext
    return _trans.gettext(message)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/translation/trans_real.py", line 379, in gettext
    _default = _default or translation(settings.LANGUAGE_CODE)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/translation/trans_real.py", line 292, in translation
    _translations[language] = DjangoTranslation(language)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/django/utils/translation/trans_real.py", line 169, in __init__
    self._add_installed_apps_translations()
  File "/usr/lib/python3/dist-packages/django/utils/translation/trans_real.py", line 217, in _add_installed_apps_translations
    raise AppRegistryNotReady(
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.

Most of the tests fail with these kind of stack traces.

Can you help me out with this.

@medmunds
Copy link
Contributor

It looks like you're using Python 3.11.something, right?

How are you building the django-anymail package? Anymail's standard hatchling build doesn't include the tests in the distribution, so I guess you're doing some sort of custom build?

How are you running the tests? Anymail's own runtests.py? tox? Something custom? Any relevant environment settings?

Have you modified anything in the tests or in Anymail?

How are you getting Django its settings? If you're using Anymail's runtests.py or tox, it will provide appropriate settings (if you've included tests/test_settings in your distribution) and will identify the module it's using at the start of the test output. If you're using your own test runner, what settings are you using, and how are you telling Django where to find them? (Did you include "anymail" in the INSTALLED_APPS?)

What other Python packages are installed? Have you installed the packages specified by tests/requirements.txt? (That wouldn't cause these errors, but it will cause others.)

If none of that helps, I'd suggest carefully comparing your test environment with a successful runs to see what differs. Anymail was tested against Django 4.2.13 and Python 3.11.9 a few days ago in a weekly scheduled run, without error. Expand the "Test django42-py311-all" node in the logs panel to see the details of installed packages, commands, settings module, etc. (This ran against the current main branch, but I'd expect 10.3 to behave the same, with possibly a single error depending on your exact Python version. Nothing like what you're seeing.)

Finally, you might try searching the web for "AppRegistryNotReady." (I have a vague memory that error can be caused by trying to call Django functions directly from your settings module, or something along those lines, but could be misremembering.)

@akashdoppalapudi
Copy link
Author

Thanks, I was running tests using pytest runner and I set django settings using an env variable.

Now I'm using runtests.py and it fixes my issue.

@medmunds medmunds closed this as not planned Won't fix, can't repro, duplicate, stale May 19, 2024
@medmunds
Copy link
Contributor

@akashdoppalapudi glad that solved it.

Incidentally, it looks like the Debian dist is still suggesting the python-sparkpost package. That's no longer necessary. django-anymail dropped all use of python-sparkpost in v8.0 on 2020-09-11. (python-sparkpost didn't appear to be maintained.)

@akashdoppalapudi
Copy link
Author

Thanks for letting me know, I'll fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants