agconti / cookiecutter-django-rest

Build best practiced apis fast with Python3
http://agconti.github.io/cookiecutter-django-rest/
MIT License
1.55k stars 189 forks source link

AppRegistryNotReady exception on fresh install #2409

Open tcardenasc opened 1 month ago

tcardenasc commented 1 month ago

What's going wrong?

How can the cookiecutter-django-rest team reproduce the problem?

OS: Fedora Workstation 40, kernel 6.10.11-200.fc40

This traceback is exactly the same as the GitHub Actions tests, starting on https://github.com/agconti/cookiecutter-django-rest/actions/runs/10701302403/job/29666963247

2024-10-01 16:23:15 Postgres is ready! ✨ 💅
2024-10-01 16:23:16 Traceback (most recent call last):
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/base.py", line 107, in wrapper
2024-10-01 16:23:16     res = handle_func(*args, **kwargs)
2024-10-01 16:23:16           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/commands/migrate.py", line 101, in handle
2024-10-01 16:23:16     self.check(databases=[database])
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/base.py", line 486, in check
2024-10-01 16:23:16     all_issues = checks.run_checks(
2024-10-01 16:23:16                  ^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/checks/registry.py", line 88, in run_checks
2024-10-01 16:23:16     new_errors = check(app_configs=app_configs, databases=databases)
2024-10-01 16:23:16                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/checks/translation.py", line 62, in check_language_settings_consistent
2024-10-01 16:23:16     get_supported_language_variant(settings.LANGUAGE_CODE)
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/__init__.py", line 256, in get_supported_language_variant
2024-10-01 16:23:16     return _trans.get_supported_language_variant(lang_code, strict)
2024-10-01 16:23:16            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 535, in get_supported_language_variant
2024-10-01 16:23:16     if code.lower() in supported_lang_codes and check_for_language(code):
2024-10-01 16:23:16                                                 ^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 480, in check_for_language
2024-10-01 16:23:16     for path in all_locale_paths()
2024-10-01 16:23:16                 ^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 457, in all_locale_paths
2024-10-01 16:23:16     for app_config in apps.get_app_configs():
2024-10-01 16:23:16                       ^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/apps/registry.py", line 147, in get_app_configs
2024-10-01 16:23:16     self.check_apps_ready()
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/apps/registry.py", line 138, in check_apps_ready
2024-10-01 16:23:16     raise AppRegistryNotReady("Apps aren't loaded yet.")
2024-10-01 16:23:16 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
2024-10-01 16:23:16 
2024-10-01 16:23:16 During handling of the above exception, another exception occurred:
2024-10-01 16:23:16 
2024-10-01 16:23:16 Traceback (most recent call last):
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 217, in _add_installed_apps_translations
2024-10-01 16:23:16     app_configs = reversed(apps.get_app_configs())
2024-10-01 16:23:16                            ^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/apps/registry.py", line 147, in get_app_configs
2024-10-01 16:23:16     self.check_apps_ready()
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/apps/registry.py", line 138, in check_apps_ready
2024-10-01 16:23:16     raise AppRegistryNotReady("Apps aren't loaded yet.")
2024-10-01 16:23:16 django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
2024-10-01 16:23:16 
2024-10-01 16:23:16 During handling of the above exception, another exception occurred:
2024-10-01 16:23:16 
2024-10-01 16:23:16 Traceback (most recent call last):
2024-10-01 16:23:16   File "/code/./manage.py", line 30, in <module>
2024-10-01 16:23:16     main()
2024-10-01 16:23:16   File "/code/./manage.py", line 26, in main
2024-10-01 16:23:16     execute_from_command_line(sys.argv)
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
2024-10-01 16:23:16     utility.execute()
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
2024-10-01 16:23:16     self.fetch_command(subcommand).run_from_argv(self.argv)
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
2024-10-01 16:23:16     self.execute(*args, **cmd_options)
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/base.py", line 459, in execute
2024-10-01 16:23:16     output = self.handle(*args, **options)
2024-10-01 16:23:16              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/core/management/base.py", line 110, in wrapper
2024-10-01 16:23:16     translation.activate(saved_locale)
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/__init__.py", line 182, in activate
2024-10-01 16:23:16     return _trans.activate(language)
2024-10-01 16:23:16            ^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 305, in activate
2024-10-01 16:23:16     _active.value = translation(language)
2024-10-01 16:23:16                     ^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 294, in translation
2024-10-01 16:23:16     _translations[language] = DjangoTranslation(language)
2024-10-01 16:23:16                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 171, in __init__
2024-10-01 16:23:16     self._add_installed_apps_translations()
2024-10-01 16:23:16   File "/usr/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 219, in _add_installed_apps_translations
2024-10-01 16:23:16     raise AppRegistryNotReady(
2024-10-01 16:23:16 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.

Is this a problem with a fresh install of the project?

maximiliancw commented 1 month ago

+1

Even with

  # General
  APPEND_SLASH = False
  TIME_ZONE = 'UTC'
  LANGUAGE_CODE = 'en-us'
  # If you set this to False, Django will make some optimizations so as not
  # to load the internationalization machinery.
  USE_I18N = False
  USE_L10N = False
  USE_TZ = True
  LOGIN_REDIRECT_URL = '/'

in settings.py, it won't run in my case.

tcardenasc commented 1 month ago

We found a simple fix! Just change the docker python version from 3.12-slim to 3.12.4-slim in the Dockerfile

FROM python:3.12.4-slim as base

maximiliancw commented 1 month ago

Confirmed. Thanks!

danielju91 commented 1 week ago

I MIGHT have found the cause of this issue. I wasn't really able to thoroughly test this but here is what I found.

When I first experienced the error, I fixed it by changing the Dockerfile as mentioned.

However, later after I have progressed with adding some apps and other objects, when I tried to run in venv: python manage.py makemigrations

I was getting a similar error (not sure if exactly the same. Computer name and part of path obfuscated)

(venv) user@COMPUTER:~$ python manage.py makemigrations
Traceback (most recent call last):
  File "/.../manage.py", line 30, in <module>
    main()
  File "/.../manage.py", line 26, in main
    execute_from_command_line(sys.argv)
  File "/.../venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/.../venv/lib/python3.12/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/.../venv/lib/python3.12/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/.../venv/lib/python3.12/site-packages/django/core/management/base.py", line 454, in execute
    self.check()
  File "/.../venv/lib/python3.12/site-packages/django/core/management/base.py", line 486, in check
    all_issues = checks.run_checks(
                 ^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/core/checks/registry.py", line 88, in run_checks
    new_errors = check(app_configs=app_configs, databases=databases)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/core/checks/translation.py", line 62, in check_language_settings_consistent
    get_supported_language_variant(settings.LANGUAGE_CODE)
  File "/.../venv/lib/python3.12/site-packages/django/utils/translation/__init__.py", line 256, in get_supported_language_variant
    return _trans.get_supported_language_variant(lang_code, strict)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 535, in get_supported_language_variant
    if code.lower() in supported_lang_codes and check_for_language(code):
                                                ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 480, in check_for_language
    for path in all_locale_paths()
                ^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/utils/translation/trans_real.py", line 457, in all_locale_paths
    for app_config in apps.get_app_configs():
                      ^^^^^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/apps/registry.py", line 147, in get_app_configs
    self.check_apps_ready()
  File "/.../venv/lib/python3.12/site-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.

I looked everywhere for a solution. But then I realized trying to run Django locally without Docker (using python manage.py runserver, would return CommandError: You must set settings.ALLOWED_HOSTS if DEBUG is False., suggesting that the settings from common.py and local.py were not being imported correctly. So, to troubleshoot, I modified the manage.py in my primary app folder (in my case backend_main) like below:

#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

def main():
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "backend_main.config")
    os.environ.setdefault("DJANGO_CONFIGURATION", "Local")

    try:
        from configurations.management import execute_from_command_line
        from django.conf import settings  # Import settings here

    except ImportError:
        # The above import may fail for some other reason. Ensure that the
        # issue is really that Django is missing to avoid masking other
        # exceptions on Python 2.
        try:
            import django  # noqa
        except ImportError:
            raise ImportError(
                "Couldn't import Django. Are you sure it's installed and "
                "available on your PYTHONPATH environment variable? Did you "
                "forget to activate a virtual environment?"
            )
        raise

        # Print settings
    for setting in dir(settings):
        if setting.isupper():
            print(f"{setting}: {getattr(settings, setting)}")

    execute_from_command_line(sys.argv)

if __name__ == '__main__':
    main()

When I did that, quite accidentally because I was trying to import django.conf.settings, it revealed that the problem was that there was a problem with importing settings from django.conf because of a missing package. Below was printed in the terminal:

Traceback (most recent call last):
  File "/.../manage.py", line 39, in <module>
    main()
  File "/.../manage.py", line 30, in main
    for setting in dir(settings):
                   ^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/utils/functional.py", line 251, in inner
    self._setup()
  File "/.../venv/lib/python3.12/site-packages/django/conf/__init__.py", line 68, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../venv/lib/python3.12/site-packages/django/conf/__init__.py", line 166, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 919, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 879, in _load_backward_compatible
  File "/.../venv/lib/python3.12/site-packages/configurations/importer.py", line 152, in load_module
    self.spec.loader.exec_module(mod)
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/.../backend_main/config/__init__.py", line 1, in <module>
    from .local import Local  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/.../backend_main/config/local.py", line 2, in <module>
    from .common import Common
  File "/.../backend_main/config/common.py", line 3, in <module>
    from distutils.util import strtobool
ModuleNotFoundError: No module named 'distutils'

So, it turns distutils was removed in Python3.12, replaced by setuptools, which is not included in the requirements.txt of this cookiecutter project yet. This might be the reason why newly scaffolded projects are experiencing this problem.

When I installed setuptools (pip install setuptools) in the venv, the issue went away and I was able to python manage.py makemigrations and python manage.py runserver without problem.

I have not yet verified if running the Dockerfile with FROM python:3.12-slim as base with setuptools included in the requirements.txt resolves the original "AppRegistry Not Ready" issue. My guess is this might have been the cause from the beginning, where deprecated packages (distutils or some other package) that are causing issues during imports just showing up as "AppRegistry Not Ready." The error handling for this message does not seem very particular under the hood. If nothing else, maybe a method like this could be useful for pinpointing the issue.

I hope this will be helpful in sorting this issue out.