Upgrading to Django 1.7: Getting AppRegistryNotReady for translation infrastructure - python

I'm upgrading from Django 1.6 to 1.7 and when I try to do manage.py runserver I get the following trace:
Traceback (most recent call last):
File "manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/apps/config.py", line 87, in create
module = import_module(entry)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/ben/Code/Repos/myrepo/myproject/core/mail/__init__.py", line 6, in <module>
from myproject.core.mail.models import IMapEmailMessage, EmailStatus
File "/home/ben/Code/Repos/myrepo/myproject/core/mail/models.py", line 20, in <module>
from myproject.core.mail.utils import render_templates
File "/home/ben/Code/Repos/myrepo/myproject/core/mail/utils.py", line 19, in <module>
from myproject.core.util import clean_html
File "/home/ben/Code/Repos/myrepo/myproject/core/util.py", line 1031, in <module>
def make_url(url, text=_('here')):
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 83, in ugettext
return _trans.ugettext(message)
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 325, in ugettext
return do_translate(message, 'ugettext')
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 306, in do_translate
_default = translation(settings.LANGUAGE_CODE)
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 209, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "/home/ben/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 189, in _fetch
"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.
I haven't used the app registry before, so I assume some setup needs to be done in my apps before translations can be used. The solution I keep seeing is to add this to wsgi.py:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
However I already have those lines in there. In the file myproject/core/util.py I changed the following line:
from django.utils.translation import ugettext as _
to:
from django.utils.translation import ugettext_lazy as _
And that just moved the issue to another file which used ugettext. Is it no longer possible to use non-lazy ugettext? Or is there some setup I need to do to avoid it being evaluated at import time?

The problem with using make_url(url, text=ugettext('here')) is that the default argument for text is evaluated when the module is imported, not when the make_url function runs.
You haven't shown the code that generated the second error, so I don't know what was wrong with it.
It is fine to use ugettext inside a function (as long as that function doesn't run during import time). For example:
def make_url(url, text=None):
if text is None:
text = ugettext('here')
Note, you can still do import uggettext as _ in your code, I've just used ugettext above to be explicit.

Related

Django upgrade from 1.8 to 1.9 is breaking on model import in init

I have run into the same problem that #JohnnyQ has commented on here.
My __init__.py is referring to multiple imports that in-turn are referring to models and these are required.
The code works perfectly fine with django 1.8.x but breaks when Django is upgraded to 1.9.x.
I am guessing it to be some class-loading sequence issue. What has changed in 1.9 w.r.t to model imports in apps triggering the AppRegistryNotReady exception?
The traceback is here:
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/_pydev_bundle/pydev_monkey.py", line 729, in __call__
ret = self.original_func(*self.args, **self.kwargs)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
six.reraise(*_exception)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/usr/local/Cellar/python#2/2.7.17/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/__init__.py", line 1, in <module>
from mycompany.core import care_team
File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/care_team.py", line 8, in <module>
from mycompany.core import models as core_models
File "/Users/vinod/mycompany/SourceCodes/mycompany_server/mycompany/core/models.py", line 16, in <module>
from django.contrib.auth.models import User
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 52, in <module>
class AbstractBaseUser(models.Model):
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/db/models/base.py", line 105, in __new__
app_config = apps.get_containing_app_config(module)
File "/Users/vinod/virtualenvs/py2/lib/python2.7/site-packages/django/apps/registry.py", line 237, in get_containing_app_config
self.check_apps_ready()
File "/Users/vinod/virtualenvs/py2/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.
Django 1.9 introduced app registry, which is initialized in 3 steps as mentioned in the documentation.
First, it imports all items in INSTALLED_APPS. At this stage, code shouldn’t import any models.
Second, Django attempts to import the models submodule of each application, if they are present.
Third, Django runs .ready() on each app config.
In core/__init__.py, there is care_team import which is, in turn, importing this from django.contrib.auth.models import User. This model import should be avoided at app level initialization.
This should be loaded lazily. Or code needs to be refactored such that no model will be imported during app initialization.

geodjango with mysql database

I am developing an application with geodjango and I have been running into some difficulties. following the procedures on the official django website https://docs.djangoproject.com/en/1.11/ref/contrib/gis/tutorial/#use-ogrinfo-to-examine-spatial-data . I first used the orginfo to check spatial data I got a failed message
FAILURE:
Unable to open datasource `world/data/TM_WORLD_BORDERS-0.3.shp' with the following drivers.
then I followed the remaining process creating the models and the error I got when I ran migration was
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/Olar/Desktop/arbithub/src/geolocation/models.py", line 5, in <module>
from django.contrib.gis.db import models
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module>
from django.contrib.gis.db.models.aggregates import * # NOQA
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/db/models/aggregates.py", line 1, in <module>
from django.contrib.gis.db.models.fields import ExtentField
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module>
from django.contrib.gis import forms, gdal
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module>
from .fields import ( # NOQA
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/forms/fields.py", line 4, in <module>
from django.contrib.gis.geos import GEOSException, GEOSGeometry
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/geos/__init__.py", line 18, in <module>
HAS_GEOS = geos_version_info()['version'] >= '3.3.0'
File "/Users/Olar/Desktop/arbithub/lib/python2.7/site-packages/django/contrib/gis/geos/libgeos.py", line 196, in geos_version_info
raise GEOSException('Could not parse version info string "%s"' % ver)
django.contrib.gis.geos.error.GEOSException: Could not parse version info string "3.6.2-CAPI-1.10.2 4d2925d6"
.
further codes would b supplied based on request. Kindly help with it
The Could not parse version info string error is the issue in ticket 28441, which has been fixed in Django 1.11.5.
Note that it's always a good idea to use the latest point release (currently 1.11.5 for 1.11.X) to make sure you've got the latest security patches and bug fixes.

AttributeError: 'Settings' object has no attribute 'OSCAR_REQUIRED_ADDRESS_FIELDS'

i have this issue in django oscar when i execute python manage.py migrate i am a beginner in django oscar .any help and suggestions are welcome.
below is my error message.
#localhost production1]$ python manage.py makemigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/usr/lib64/python2.7/site-packages/django/core/management/__init__.py", line 337, in execute
django.setup()
File "/usr/lib64/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib64/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/lib64/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/usr/lib/python2.7/site-packages/oscar/apps/address/models.py", line 1, in <module>
from oscar.apps.address.abstract_models import (
File "/usr/lib/python2.7/site-packages/oscar/apps/address/abstract_models.py", line 19, in <module>
class AbstractAddress(models.Model):
File "/usr/lib/python2.7/site-packages/oscar/apps/address/abstract_models.py", line 35, in AbstractAddress
POSTCODE_REQUIRED = 'postcode' in settings.OSCAR_REQUIRED_ADDRESS_FIELDS
File "/usr/lib64/python2.7/site-packages/django/conf/__init__.py", line 57, in __getattr__
val = getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'OSCAR_REQUIRED_ADDRESS_FIELDS'
by docs
The last addition to the settings file is to import all of Oscar’s default settings:
from oscar.defaults import *
more details install-by-hand, hope it help you

500 Server Error on GAE - MySQLdb not loaded

I get a 500 Server Error while uploading my app on GAE. It works perfectly fine locally with dev_appserver with SETTINGS_MODE='prod'.
Based on the logs, it cannot find MySQLdb on GAE. It does not make sense to me.
Help!
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 266, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/wsgi.py", line 236, in __call__
self.load_middleware()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/core/handlers/base.py", line 49, in load_middleware
mod = import_module(mw_module)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/contrib/auth/middleware.py", line 3, in <module>
from django.contrib.auth.backends import RemoteUserBackend
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/contrib/auth/backends.py", line 3, in <module>
from django.contrib.auth.models import Permission
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/contrib/auth/models.py", line 8, in <module>
from django.db import models
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/db/utils.py", line 93, in __getitem__
backend = load_backend(db['ENGINE'])
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/db/utils.py", line 27, in load_backend
return import_module('.base', backend_name)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.5/django/db/backends/mysql/base.py", line 17, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
The answer is fairly simple. Add the following lines in the app.yaml file:
libraries:
- name: MySQLdb
version: "latest"
Details can be found on the page
https://cloud.google.com/appengine/docs/python/cloud-sql

Django-dash memo and news plugin error

I am trying to setup a django-dash demo via: https://github.com/barseghyanartur/django-dash.
I have manually installed all the dependencies for django-dash (because of work computer/proxy issues) and supposedly successfully installed django-dash as well. Following the tutorial, I went to add several layouts and plugins to the INSTALLED_APPS settings for my app. There are no issues when I add all of the plugins except for the ones in asterisks - memo and news.
INSTALLED_APPS = (
# ...
'dash',
'dash.contrib.layouts.android',
'dash.contrib.layouts.bootstrap2',
'dash.contrib.layouts.windows8',
'dash.contrib.plugins.dummy',
'dash.contrib.plugins.image',
**'dash.contrib.plugins.memo',
'dash.contrib.plugins.news',**
'dash.contrib.plugins.rss_feed',
'dash.contrib.plugins.url',
'dash.contrib.plugins.video',
'dash.contrib.plugins.weather',
# ...
)
This is the error message I get. I thought maybe files were missing but when I check the folders, autoreload.py etc are all there. What might be the issue?
Unhandled exception in thread started by <function wrapper at 0x0208ECB0>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\django\utils\autoreload.py", line 93, in w
rapper
fn(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\core\management\commands\runserver.
py", line 101, in inner_run
self.validate(display_num_errors=True)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 310,
in validate
num_errors = get_validation_errors(s, app)
File "C:\Python27\lib\site-packages\django\core\management\validation.py", lin
e 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 196, in
get_app_errors
self._populate()
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 75, in
_populate
self.load_app(app_name, True)
File "C:\Python27\lib\site-packages\django\db\models\loading.py", line 99, in
load_app
models = import_module('%s.models' % app_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py", line 40, in im
port_module
__import__(name)
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\models.py
", line 21, in <module>
class DashboardSettings(models.Model):
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\models.py
", line 32, in DashboardSettings
layout_uid = models.CharField(_("Layout"), max_length=25, choices=get_regist
ered_layouts())
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\base.py",
line 1491, in get_registered_layouts
ensure_autodiscover()
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\base.py",
line 1432, in ensure_autodiscover
autodiscover()
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\discover.
py", line 33, in autodiscover
do_discover(PLUGINS_MODULE_NAME)
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\discover.
py", line 27, in do_discover
__import__('{0}.{1}'.format(app, module_name))
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\contrib\p
lugins\memo\dash_plugins.py", line 10, in <module>
from dash.contrib.plugins.memo.forms import MemoForm, TinyMCEMemoForm
File "C:\Python27\lib\site-packages\django_dash-0.4.5-py2.7.egg\dash\contrib\p
lugins\memo\forms.py", line 9, in <module>
from tinymce.widgets import TinyMCE
File "C:\Python27\lib\site-packages\django_tinymce-1.5.2-py2.7.egg\tinymce\wid
gets.py", line 10, in <module>
import tinymce.settings
File "C:\Python27\lib\site-packages\django_tinymce-1.5.2-py2.7.egg\tinymce\set
tings.py", line 16, in <module>
JS_ROOT = getattr(settings, 'TINYMCE_JS_ROOT',os.path.join(settings.STATIC_R
OOT, 'tiny_mce'))
File "C:\Python27\lib\ntpath.py", line 64, in join
result_drive, result_path = splitdrive(path)
File "C:\Python27\lib\ntpath.py", line 94, in splitdrive
if p[1:2] == ':':
TypeError: 'NoneType' object has no attribute '__getitem__'
As documented (recently), django-tinymce package is required for news and memo plugins.
https://github.com/barseghyanartur/django-dash/tree/master/src/dash/contrib/plugins/news
https://github.com/barseghyanartur/django-dash/tree/master/src/dash/contrib/plugins/memo
Unfortunately, django-tinymce is not yet compatible with Python 3 (there is, however, a python 3 compatible branch which is aimed to become master one day).
In case you're using Python 3, you should simply use TinyMCE without django-tinymce (which isn't much work anyway).

Categories

Resources