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

AppRegistryNotReady raised if us_states imported in app config ready() method #187

Open
rush340 opened this issue Dec 3, 2015 · 6 comments

Comments

@rush340
Copy link

rush340 commented Dec 3, 2015

If the us_states module is imported within an AppConfig's ready() method, then an AppRegistryNotReady exception will be raised. The sorting for STATE_CHOICES and USPS_CHOICES is forcing resolution of the lazily translated state names at import time, but i18n isn't available yet.

In our case we're importing a view class in our ready() method to read a property off of it. Our views module imports something from models where we have a model that's using the USStateField which causes the import of us_states.

@claudep
Copy link
Member

claudep commented Dec 3, 2015

Could you please paste the traceback?

@rush340
Copy link
Author

rush340 commented Dec 3, 2015

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/core/management/__init__.py", line 342, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/core/management/__init__.py", line 176, in fetch_command
    commands = get_commands()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/lru_cache.py", line 100, in wrapper
    result = user_function(*args, **kwds)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/core/management/__init__.py", line 71, in get_commands
    for app_config in reversed(list(apps.get_app_configs())):
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/apps/registry.py", line 137, in get_app_configs
    self.check_apps_ready()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/apps/registry.py", line 124, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

@rush340
Copy link
Author

rush340 commented Dec 3, 2015

It looks like the import works if it's done within the ready() function, so it seems like Django imports the app registry classes before i18n is initialized, but i18n is initialized by the time the app config's ready() method is run.

@claudep
Copy link
Member

claudep commented Dec 3, 2015

I think that's the expected behavior. But could you try to generate the traceback once more with ./manage.py runserver --noreload and paste it if it's different?

@rush340
Copy link
Author

rush340 commented Dec 3, 2015

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/core/management/__init__.py", line 324, in execute
    django.setup()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/michael/test/localflavor/localflavortestproject/testapp/apps.py", line 2, in <module>
    from localflavor.us import us_states
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/localflavor/us/us_states.py", line 157, in <module>
    STATE_CHOICES = tuple(sorted(US_STATES + US_TERRITORIES + ARMED_FORCES_STATES, key=lambda obj: obj[1]))
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/functional.py", line 147, in __lt__
    other = other.__cast()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/functional.py", line 126, in __cast
    return self.__text_cast()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/functional.py", line 114, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 85, in ugettext
    return _trans.ugettext(message)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 319, in ugettext
    return do_translate(message, 'ugettext')
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 296, in do_translate
    _default = _default or translation(settings.LANGUAGE_CODE)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 203, in translation
    _translations[language] = DjangoTranslation(language)
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 113, in __init__
    self._add_installed_apps_translations()
  File "/Users/michael/Envs/localflavor/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 161, in _add_installed_apps_translations
    "The translation infrastructure cannot be initialized before the "
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.

@claudep
Copy link
Member

claudep commented Dec 3, 2015

Thanks.
It might be possible to wrap STATE_CHOICES and USPS_CHOICES inside a django.utils.functional.SimpleLazyObject. Not sure if it could have unwanted side-effect...

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