AppRegistryNotReady: The translation infrastructure cannot be initialized
Asked Answered
E

10

28

When I try to access to my app, I'm getting the following error below:

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

Here is my wsgi.py file:

"""                                                                                                                                                                                     
WSGI config for Projectizer project.                                                                                                                                                    
                                                                                                                                                                                        
It exposes the WSGI callable as a module-level variable named ``application``.                                                                                                          
                                                                                                                                                                                        
For more information on this file, see                                                                                                                                                  
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/                                                                                                                            
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Projectizer.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

And here is the stacktrace.

mod_wsgi (pid=28928): Exception occurred processing WSGI script '/var/www/projectizer/apache/django.wsgi'.

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__

    response = self.get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 199, in get_response

    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 236, in handle_uncaught_exception

    return debug.technical_500_response(request, *exc_info)

File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 91, in technical_500_response

    html = reporter.get_traceback_html()

File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 350, in get_traceback_html

    return t.render(c)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 148, in render

    return self._render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 142, in _render

    return self.nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 844, in render

    bit = self.render_node(node, context)

File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 80, in render_node

    return node.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/debug.py", line 90, in render

    output = self.filter_expression.resolve(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py", line 624, in resolve

    new_obj = func(obj, *arg_vals)

File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 769, in date

    return format(value, arg)

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 343, in format

    return df.format(format_string)

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format

    pieces.append(force_text(getattr(self, piece)()))

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 268, in r

    return self.format('D, j M Y H:i:s O')

File "/usr/local/lib/python2.7/dist-packages/django/utils/dateformat.py", line 35, in format

    pieces.append(force_text(getattr(self, piece)()))

File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 85, in force_text

    s = six.text_type(s)

File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 144, in __text_cast

    return func(*self.__args, **self.__kw)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/__init__.py", line 83, in ugettext

    return _trans.ugettext(message)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 325, in ugettext

    return do_translate(message, 'ugettext')

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 306, in do_translate

    _default = translation(settings.LANGUAGE_CODE)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 209, in translation

    default_translation = _fetch(settings.LANGUAGE_CODE)

File "/usr/local/lib/python2.7/dist-packages/django/utils/translation/trans_real.py", line 189, in _fetch

    "The translation infrastructure cannot be initialized before the "

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.
Earldom answered 24/12, 2014 at 0:59 Comment(2)
I came across this error when I was importing stuff in the app's __init__.py which ultimately tried to import models.py which imports ugettext.Oira
@Oira Thanks for pointing this out, I was getting same error, moved imports from __init__.py to models.py to resolve the issue.Malinda
T
30

I faced the same error. Following worked for me. In your wsgi file change the last line to :

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

This have been changed since Django 1.6 to newer version. Here is the post that helped to deploy the django app.

If you want to use Nginx as webserver to deploy django app follow this post.

Trackless answered 16/4, 2015 at 6:26 Comment(2)
Thanks, Ajeet. I have been facing this issue for a long time and resolve it by your answer.Development
Hi. I don't see any difference with the author's wsgi.py file. I'm using DJango 2 and the wsgi.py configuration is the same that you have recomended :(Hesitation
A
12

This is an answer for the less clever ones (like me): Be sure to check the obvious: The error message says: ... Check that you don't make non-lazy gettext calls at import time. So, if you use django's translation in the verbose_name of a model field or on any other part that is evaluated at import time, you need to use the *_lazy version. If not, you'll end up with the error the OP had.

I basically had:

from django.db import models
from django.utils.translation import gettext as _
import datetime
# other things

class myModle(models.Model):
    date = models.DateField(_('Date'), default=datetime.date.today)
    # other defs. and things

And got the same error as the OP, but my wsgi config was fine.

All I had to do was replacing gettext with gettext_lazy (or ugettext with ugettext_lazy) and everything was fine.

Altarpiece answered 8/5, 2017 at 23:14 Comment(0)
I
7

@hellsgate solution worked for me.

Specifically from the link referenced by @hellsgate, I changed:

module = django.core.handlers.wsgi:WSGIHandler()

to

module = django.core.wsgi:get_wsgi_application()

in my vassals.ini file

Isolation answered 29/8, 2015 at 14:9 Comment(0)
I
6

Here is another possible cause for that exception: in my apps.py file I had accidentally added translation for the name of the AppConfig class:

class BookConfig(AppConfig):
    name = _('Book')
    verbose_name = _('Book')

After removing the misplaced translation everything started to work perfectly:

class BookConfig(AppConfig):
    name = 'Book'
    verbose_name = _('Book')
Impotent answered 30/11, 2019 at 21:6 Comment(0)
P
4

This appears to be the same as this incorrectly reported bug - https://code.djangoproject.com/ticket/23146.

I came across this as well and the fix suggested in that link worked out for me. The update needs to be made in your wsgi.py file. If you aren't sure how to make the change, post 'wsgi.py' for me to have look at

Pythagoreanism answered 5/1, 2015 at 16:42 Comment(0)
D
3

You might be missing setting up path to your application. Check this out my wsgi file. You will find more accurate documentation here https://joshcarllewis.com/articles/getting-started-with-django . I hope it will resolve your problem.

import os, sys

sys.path.append('D:/django/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Development answered 2/9, 2015 at 14:8 Comment(1)
By using the sys.path.append in this way, I was able to make the wsgi file referenced at blog.dscpl.com.au/2008/12/… work!!Alarice
P
1

Using

"from django.utils.translation import gettext_lazy as _"

instead of

"from django.utils.translation import gettext as _"

in apps.py files inside my app folders solved my problem.

Parity answered 10/12, 2022 at 9:6 Comment(0)
A
0

You can see what version of "Django" you have installed with:

$python -c 'import django; print (django.get_version ())'

and see that version uses the project "requeriments.txt". If it is not the same version, you have to uninstall "Django" and install the version set to "requeriments.txt".

If you use "virtual environment", may thou standest wrong "virtual environment" and you installed it a new version of django. for example:

In your "requeriments.txt" put Django == 1.6.1

$python -c 'import django; print(django.get_version())' 
1.7.4 
$sudo pip uninstall Django 
$sudo pip install Django==1.6.1
Alliteration answered 23/3, 2015 at 9:42 Comment(0)
S
0

Same answer as @hellsgate and @shawn. I had to replace

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

by

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Subir answered 21/9, 2017 at 23:0 Comment(0)
A
0

I got the same error below:

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.

Because I used gettext() for verbose_name in MyAppConfig class in my_app/apps.py as shown below:

# "my_app/apps.py"

from django.apps import AppConfig
from django.utils.translation import gettext as _ # Here

class MyAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'my_app'
    verbose_name = _('my_app') # Here

And, I used gettext() in LANGUAGES in core/settings.py as shown below:

# "core/settings.py"

from django.utils.translation import gettext as _ # Here

LANGUAGES = (
    ('en', _('English')),
    ('fr', _('French'))
)

So instead, I used gettext_lazy() for verbose_name in MyAppConfig class in my_app/apps.py as shown below, then the error was solved:

# "my_app/apps.py"

from django.apps import AppConfig
from django.utils.translation import gettext_lazy as _ # Here

class MyAppConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'my_app'
    verbose_name = _('my_app') # Here

And, I used gettext_lazy() in LANGUAGES in core/settings.py as shown below, then the error was solved:

# "core/settings.py"

from django.utils.translation import gettext_lazy as _ # Here

LANGUAGES = (
    ('en', _('English')), # Here
    ('fr', _('French')) # Here
)
Ardyth answered 16/6, 2023 at 11:1 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.