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

Error - "The translation infrastructure cannot be initialized before the " #203

Closed
chadsaun opened this issue Mar 8, 2016 · 10 comments
Closed
Labels

Comments

@chadsaun
Copy link

chadsaun commented Mar 8, 2016

I just installed localflavor and then added the widgets part to my form.

class Meta:
        model = Address
        exclude = []
        widgets = {
            'country_area': USStateSelect()
        }

Then I get this error below. But as soon as I comment out the widgets part from my form, the error goes away. Any ideas?

"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 Mar 8, 2016

It's generally a symptom of a wrong import order in your project. Maybe the full traceback could give us a hint about what's happening.

@chadsaun
Copy link
Author

chadsaun commented Mar 8, 2016

Here is the traceback...

Unhandled exception in thread started by <pydev_monkey._NewThreadStartupWithTrace instance at 0x10575ab90>
Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/helpers/pydev/pydev_monkey.py", line 521, in __call__
    return self.original_func(*self.args, **self.kwargs)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/apps/config.py", line 86, 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/chad/Development/website/rbi-com-saleor/server/saleor/checkout/__init__.py", line 8, in <module>
    from .steps import (BillingAddressStep, ShippingStep, DigitalDeliveryStep,
  File "/Users/chad/Development/website/rbi-com-saleor/server/saleor/checkout/steps.py", line 25, in <module>
    from .forms import (DigitalDeliveryForm, DeliveryForm, OrganizationForm, CreditCardPaymentForm,
  File "/Users/chad/Development/website/rbi-com-saleor/server/saleor/checkout/forms.py", line 12, in <module>
    from ..userprofile.forms import AddressForm
  File "/Users/chad/Development/website/rbi-com-saleor/server/saleor/userprofile/forms.py", line 64, in <module>
    class AddressForm(forms.ModelForm):
  File "/Users/chad/Development/website/rbi-com-saleor/server/saleor/userprofile/forms.py", line 74, in AddressForm
    class Meta:
  File "/Users/chad/Development/website/rbi-com-saleor/server/saleor/userprofile/forms.py", line 78, in Meta
    'country_area': USStateSelect()
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/localflavor/us/forms.py", line 141, in __init__
    from .us_states import STATE_CHOICES
  File "/Users/chad/.virtualenvs/website/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/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/functional.py", line 168, in __lt__
    other = other.__cast()
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/functional.py", line 152, in __cast
    return self.__text_cast()
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/functional.py", line 140, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 84, in ugettext
    return _trans.ugettext(message)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 328, in ugettext
    return do_translate(message, 'ugettext')
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 305, in do_translate
    _default = _default or translation(settings.LANGUAGE_CODE)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 207, in translation
    _translations[language] = DjangoTranslation(language)
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 117, in __init__
    self._add_installed_apps_translations()
  File "/Users/chad/.virtualenvs/website/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 165, 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 Mar 8, 2016

OK, thanks, this shows the bug is valid. STATE_CHOICES and USPS_CHOICES are sorting their entries by translated string at loading time, hence defeating the purpose of gettext_lazy.

@claudep claudep added the bug label Mar 8, 2016
@claudep
Copy link
Member

claudep commented Mar 8, 2016

Note that this will be difficult to fix if we want to keep the sorting functionality, as the choices are consumed very soon in the form creation process.

@vladimirnani
Copy link
Contributor

@chadsaun Hey. Im unable to reproduce this issue. Could you check that you have
application = get_wsgi_application() instead of application = WSGIHandler() in your wsgi.py please.

@stugots
Copy link

stugots commented Apr 29, 2016

I'm seeing this error too. My wsgi.py has application = get_wsgi_application().

@claudep
Copy link
Member

claudep commented Apr 30, 2016

If this is happening because of an entry in the widgets attribute of a Meta form class, you should provide a select class instead of a select instance (USStateSelect instead of USStateSelect()). Of course, this doesn't work when you have to pass arguments to the widget.

@lissizza
Copy link

Have the same bug. I tried to migrate from the old version django_localflavor_* to the new localflavor. After changing import strings I received the same errorr:

$ ./manage.py runserver
Unhandled exception in thread started by <function wrapper at 0x7fb5b56a3398>
Traceback (most recent call last):
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 107, in inner_run
    autoreload.raise_last_exception()
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 252, in raise_last_exception
    six.reraise(*_exception)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 229, in wrapper
    fn(*args, **kwargs)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/apps/config.py", line 86, in create
    module = import_module(entry)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/fox/dev/rmbo/rambo/__init__.py", line 1, in <module>
    import tasks, checks
  File "/home/fox/dev/rmbo/rambo/tasks.py", line 10, in <module>
    from clients.models import DataSource
  File "/home/fox/dev/rmbo/clients/models.py", line 7, in <module>
    from localflavor.us import models
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/localflavor/us/models.py", line 5, in <module>
    from .us_states import STATE_CHOICES, USPS_CHOICES
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/localflavor/us/us_states.py", line 156, in <module>
    STATE_CHOICES = tuple(sorted(US_STATES + US_TERRITORIES + ARMED_FORCES_STATES, key=lambda obj: obj[1]))
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/functional.py", line 168, in __lt__
    other = other.__cast()
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/functional.py", line 152, in __cast
    return self.__text_cast()
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/functional.py", line 140, in __text_cast
    return func(*self.__args, **self.__kw)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 84, in ugettext
    return _trans.ugettext(message)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 330, in ugettext
    return do_translate(message, 'ugettext')
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 307, in do_translate
    _default = _default or translation(settings.LANGUAGE_CODE)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 209, in translation
    _translations[language] = DjangoTranslation(language)
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 118, in __init__
    self._add_installed_apps_translations()
  File "/home/fox/.virtualenvs/rmbo/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 159, 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.

Any import, even simle from localflavor.us import models gives this result. The old variant works perfectly.

@Unitedworth
Copy link

I am also encountering this but only when I run celery on Heroku. Does not happen when I run celery locally. Sounds like I may have a setup issue but I am not sure where. Any ideas for me?

@claudep
Copy link
Member

claudep commented Aug 19, 2016

Only looking at the traceback can show what is possible to prevent this error. Generally, try to not trigger import of localflavor stuff in the init file of any app.

PaulSD added a commit to PaulSD/tendenci that referenced this issue Dec 19, 2016
The wsgi loader calls django.setup(), which loads the haystack app,
which calls QueuedSignalProcessor.setup() in
tendenci/apps/search/signals.py, which imports search_indexes.py in the
forms_builder Tendenci app, which imports models.py, which imports
localflavor.us.us_states from Django.

localflavor.us.states versions 1.2 and 1.3 have a bug that causes
an exception if the module is imported during Django initialization.
See: django/django-localflavor#203

This breaks the search index for the forms_builder Tendenci app.

localflavor.us.states is not actually needed by search_indexes.py, so
simply ignore the exception that is thrown when it is imported during
Django initialization.
benkonrath pushed a commit that referenced this issue Jan 3, 2017
Commit 4369237 introduced lazy translations for US state names.
However, US_STATES, STATE_CHOICES, and USPS_CHOICES are sorted at import
time based on the translated state names.  This causes two problems:
1) If localflavor.us is imported during Django initialization, then an
   exception will be thrown because translations cannot be made during
   Django initialization.
   (See #203)
2) When the run-time translations are different than the import-time
   translations, US_STATES, STATE_CHOICES, and USPS_CHOICES are not
   re-sorted based on the run-time translations, so those lists may be
   mis-sorted.

This commit corrects these problems by lazily sorting US_STATES,
STATE_CHOICES, and USPS_CHOICES.

Unfortunately, while this works as expected in most cases, the lazy
evaluation may not work as expected in every case due to internal tuple
optimizations in the Python interpreter.  For example,
`US_STATES + ('XX', _('Select a State'))` works as expected, but
`('XX', _('Select a State')) + US_STATES` throws
'TypeError: can only concatenate tuple (not "__proxy__") to tuple'
because Python concatenates the tuples entirely in C code without making
any Python code calls to the US_STATES object, which prevents US_STATES
from being lazily generated before the concatenation is processed.  To
work around this, you can use a slice index to force a Python code
call on the object before any other operations are processed:
`('XX', _('Select a State')) + US_STATES[:]`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants