Python version : 2.7.10
Django version : 1.8
Environment : Virtual environment
Problem: Whenever i tried to run ./manage.py runserver or shell I get this error
"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."
Based on some responses on some related posts, I have also checked my wsgi file and it has the updated way of referencing the wsgi application. Here's how my wsgi file looks:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "instant_reports.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Any help/guidance on resolving this ?
Did you use ugettext() in your code? Change it to ugettext_lazy().Quoting Django official documentation:
AppRegistryNotReady: This happens when importing an application configuration or a models module triggers code that depends on the app registry.
For example, ugettext() uses the app registry to look up translation
catalogs in applications. To translate at import time, you need
ugettext_lazy() instead. (Using ugettext() would be a bug, because the
translation would happen at import time, rather than at each request
depending on the active language.)
You need to use the lazy translation in your settings.py and any file (views.py, models.py) that might get imported while Django is boostrapped.
Related
Recently I discovered Dynaconf which is a nice configuration management package that integrates nicely with Flask and Django. The Django app is running wonderfully with Dynaconf. However the Celery app that my App depends on to run background tasks is not.
Here is the code for the configuration of the Celery app that was working before using Dynaconf:
from celery import Celery
app = Celery('KillerApp')
app.config_from_object('django.conf:settings', namespace='CELERY')
It seems that I need to change 'django.conf:settings' to something else. Any ideas?
You can pass in a string representing a module to import, or just pass in the configuration object directly; see the Celery.config_from_object() method documentation.
You'll have a module that sets up the Dynaconf() instance, e.g. if you have a package named acme_project with a config.py file in it with
from dynaconf import Dynaconf
settings = Dynaconf(
settings_files=['settings.toml', '.secrets.toml'],
)
then you can import acme_project.config and find the settings object there. You can either import that object or let Celery do that by using 'acme_project.config:settings' as the value you pass to app.config_from_object(). The namespace argument tells Celery to expect all settings to be prefixed with CELERY_, exactly like the way this works with Django. Use this if you plan to use the Dynaconf-managed settings to configure multiple components, not just Celery.
E.g., if you used:
app.config_from_object('acme_project.config:settings', namespace='CELERY')
then your settings.toml or settings.yaml or whatever file format you picked would need to use CELERY_ as a prefix for all the settings.
If you are using the Django plugin for Dynaconf then you are able to just use the django.conf:settings directly as Dynaconf patches the django settings object.
If you still have problems I recommend opening an issue on dynaconf repo and try using the settings of your application directly.
Example, if you have an app called foo your DJANGO_SETTINGS_MODULE might be foo.settings then you can use for celery:
app.config_from_object('foo.settings:settings')
Since the project is evolving I would like to start executing the system check framework of Django on dev environment. The technology stack is Ubuntu, PostgreSQL, Django1.9 + UWSGI. But...
django-admin check
outputs the following error:
ImportError: No module named my_project.settings
The wsgi.py file contains:
import os
import sys
from django.core.wsgi import get_wsgi_application
sys.path.append('~/virtenv/my_site')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_site.settings")
application = get_wsgi_application()
The env variable DJANGO_SETTINGS_MODULE echoes my_site.settings
It is important to mention that the settings.py file is in virtenv/my_site/my_site/ . Please also note that the entire web application is running fine, I am also using the features of manage.py. Its just the django-admin check that is getting on my nerves.
you seem to append to the sys path in the wsgi but the admin check won't be using that.
Make sure you add the project path to the sys path for the environment you're using
I'm having trouble setting up my django on dreamhost shared hosting using python passenger_wsgi.py and virtual env. When I run passenger_wsgi.py no error is returned and the shell prints my project path. My website shows a 500 International server error. I have cleared my cache so there is no cache error. How do I set this up properly?
django 1.9
python 2.7
apache
My site structure is:
/home/myuser/mydomain.com/
env/
myApp/
passenger_wsgi.py
public/
passenger_wsgi.py
import sys, os
cwd = os.getcwd()
sys.path.append(cwd)
project_location = cwd + '/myApp'
print (project_location)
sys.path.insert(0, project_location)
#Switch to new python
if sys.version < "2.7.3": os.execl("/home/myuser/mydomain.com/env/bin/python",
"python2.7.3", *sys.argv)
sys.path.insert(0,'/home/myuser/mydomain.com/env/bin')
sys.path.insert(0,'/home/myuser/mydomain.com/env/lib/python2.7/site-packages/django')
sys.path.insert(0,'/home/myuser/mydomain.com/env/lib/python2.7/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = "myApp.settings"
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
I had issues with Dreamhost specifically when upgrading from Django 1.6 to 1.8. One of the issues was with the WSGIHandler(). I can't say this is your problem specifically, but you can try setting application like this:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
Finally - make sure you restart passenger. There are docs here about how to do it: http://wiki.dreamhost.com/Passenger
From that page:
Whenever the code or configuration files for your application are modified, you must create or update the modification date of the file "tmp/restart.txt" in the application's root directory tree in order to trigger Passenger to reinitialize the application. Passenger caches many resources so changes are not recognized unless the modification date of "tmp/restart.txt" is changed.
The most common method to make this change is to run "touch tmp/restart.txt" via SSH. (Ruby on Rails automatically creates a directory named "tmp". If you are creating non-RoR application, you may need to create the "tmp" directory manually.
I am running Django in a virtual environment (using virtualenv), and I'm trying to add a custom development environment settings file to simplify app configuration when I'm developing. My plan was to do this with two lines of code
if os.environ.get('DEVELOPMENT', None):
from login import settings_dev
I've also tried import settings_def and from login.settings_dev import *. My settings_dev.py file is sitting in the same directory as my settings.py file and my app is sitting in a folder called login. When I run python login/manage.py syncdb I get this error:
Error: Import by filename is not supported.
My searching keeps bringing up DJANGO_SETTINGS_MODULE (though I'm not sure how it plays into all this - first Django app :]), so just an FYI it is set in my settings.py file like so:
os.environ['DJANGO_SETTINGS_MODULE'] = 'login.settings'
I've also tried exporting it in my terminal, but I get the same error.
Does anyone know how I can fix this/what I'm doing wrong here?
Make sure while passing relative address of file to use "." instead of "/".
I faced the same error what I actually did
"music/urls"
But it should be
"music.urls"
In the original settings.py, at the very end:
try:
from settings_dev import *
except ImportError:
pass
Create settings_dev.py in the same directory as settings.py, and in it, add these two lines at the very top:
import sys
globals().update(vars(sys.modules['settings']))
Now add whatever development settings you want in this file.
I had similar error in runserver command execution and finally I've found that this error raises because of python version incompatibility by the django version installed. There is two versions of python on my system and I had running django server by the wrong one. Hope it could be helpful to someone.
I have problems with importing correctly a module on appengine. My app generally uses django with app-engine-patch, but this part is task queues using only the webapp framework.
I need to import django settings for the app to work properly.
My script starts with:
import os
import sys
sys.path.append('common/')
# Force Django to reload its settings.
from django.conf import settings
settings._target = None
# Must set this env var before importing any part of Django
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
I always get this error, or something related:
<type 'exceptions.ImportError'>: No module named ragendja.settings_pre
because the settings.py file starts with
from ragendja.settings_pre import *
I think I need to add ragendja to sys.path again but I had several tries that didn't work.
Here is my directory:
project/
app.yaml
setting.py
common/
appenginepatch/
ragendja/
setting_pre.py
myapp/
script.py
Is it only a sys.path problem and how do I need to modify it with the correct syntax?
Thanks
App engine patch manipulates sys.path internally. Background tasks bypass that code, so your path will not be ready for Django calls. You have two choices:
Fix the paths manually. The app engine documentation (see the sub-section called "Handling import path manipulation") suggests factoring the path manipulation code into a module that can be imported by your task script.
Eliminate dependencies on django code, if possible. If you can write your task to be pure python and/or google api calls, you're good to go. In your case, this might mean refactoring your settings code.
Why not:
sys.path.append('common/appenginepatch')
since the ragendja is in this directory?