I have a script, which should run once per day by crontab. That works fine on my desktop. But when I try to run it on a virtualenv on my RPi, I get this error:
Traceback (most recent call last):
File "mailalert.py", line 7, in <module>
from django.contrib.auth.models import User
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/contrib/auth/__init__.py", line 5, in <module>
from django.middleware.csrf import rotate_token
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/middleware/csrf.py", line 16, in <module>
from django.utils.cache import patch_vary_headers
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/utils/cache.py", line 26, in <module>
from django.core.cache import get_cache
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/core/cache/__init__.py", line 70, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/conf/__init__.py", line 53, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/conf/__init__.py", line 46, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
It looks like I cant use:
from django.contrib.auth.models import User
What is the problem?
DJANGO_SETTINGS_MODULE
You need to configure the DJANGO_SETTINGS_MODULE environment variable in your mailalert.py script, prior to importing Django code.
Here's how you could do it:
#!/usr/bin/env python
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "benchmarks.settings")
# Do you thing now.
Virtualenv
Note that you code is probably not running inside a virtualenv right now, as made evidenced by the package paths (/usr/local/lib isn't your virtualenv):
"/usr/local/lib/python2.7/dist-packages/Django-1.5.4-py2.7.egg/django/contrib/auth/__init__.py"
Related
I just started using django-cms and am facing issues. I used virtualenv.please help.The following is the error:
Traceback (most recent call last):
File "create_user.py", line 4, in <module>
from django.contrib.auth import get_user_model
File "/home/mayankmodi/SSAD18/Source/env/local/lib/python2.7/site-packages
/django/contrib/auth/__init__.py", line 7, in < module>
from django.middleware.csrf import rotate_token
File "/home/mayankmodi/SSAD18/Source/env/local/lib/python2.7/site-packages
/django/middleware/csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "/home/mayankmodi/SSAD18/Source/env/local/lib/python2.7/site-packages/django/utils/cache.py", line 26, in <module>
from django.core.cache import caches
File "/home/mayankmodi/SSAD18/Source/env/local/lib/python2.7/site-packages/django/core/cache/__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "/home/mayankmodi/SSAD18/Source/env/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/mayankmodi/SSAD18/Source/env/local/lib/python2.7/site-packages
/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Can somebody tell me which specific files to edit and what edits should i make
Try running this command instead:
djangocms -w web
This will start the djangocms-installer wizard and run you through various settings, dependencies, and create a superuser for you to log in with. When it finishes, you should have a running instance of django-cms within ./web/.
You can access the new instance by going into ./web/ and running runserver as usual:
$ source venv/bin/activate
(venv) $ python manage.py runserver
I have same problem from a few week ago. I assume the new version(djangocms-installer==0.9.x) have problem because when I created a project using default djangocms-installer(==0.9.0) like "djangcms -p . mysite", there was no questions about DB, django version, or etc, then error...
Try old version "pip install djangocms-installer==0.8.x"(x=12,11,10,...)
Now, I am using "djangocms-installer==0.8.12" without any error.
i found an error while running webservice on local
Traceback (most recent call last):
File "D:\Projects\wamp\www\mysite_livebackupcode\django-apps\mysite\account\views.py", line 3, in <module>
from django.views.decorators.csrf import csrf_exempt
File "C:\Python27\Lib\site-packages\django\views\decorators\csrf.py", line 1, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
File "C:\Python27\Lib\site-packages\django\middleware\csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
File "C:\Python27\Lib\site-packages\django\utils\cache.py", line 26, in <module>
from django.core.cache import get_cache
File "C:\Python27\Lib\site-packages\django\core\cache\__init__.py", line 69, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
File "C:\Python27\Lib\site-packages\django\conf\__init__.py", line 54, in __getattr__
self._setup(name)
File "C:\Python27\Lib\site-packages\django\conf\__init__.py", line 47, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either
define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
can anybody please help me to get out of this stuff.
thanks in advance
You can set environment variable in linux for example:
export DJANGO_SETTINGS_MODULE=myproject.settings
It should help you.
Installed pyes recently , but when i tired it in the python shell it is giving the error,
Can any one please help me out in this regard:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pyes-0.19.1-py2.7.egg/pyes/__init__.py", line 41, in <module>
from .djangoutils import *
File "/usr/local/lib/python2.7/dist-packages/pyes-0.19.1-py2.7.egg/pyes/djangoutils.py", line 6, in <module>
from django.db import models
File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 52, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 45, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I found that this issue can be "worked around" by simply performing the following before running the script in Terminal
export DJANGO_SETTINGS_MODULE=default.settings
After running that code the script worked exactly the way it was supposed to!
I have the "mysite/poll" app from the Django tutorial running locally and am trying to deploy to GAE. I have defined DJANGO_SETTINGS_MODULE in the wsgi.py and admin.py as
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
This is the traceback on deployment:
Traceback (most recent call last):
File "/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 196, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "/python27_runtime/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 255, in _LoadHandler
handler = __import__(path[0])
File "/base/data/home/apps/s~ceemee11111/1.365096182895980177/admin.py", line 1, in <module>
from django.contrib import admin
File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/contrib/admin/__init__.py", line 3, in <module>
from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/contrib/admin/helpers.py", line 2, in <module>
from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
File "/python27_runtime/python27_lib/versions/third_party/django- 1.4/django/contrib/admin/util.py", line 1, in <module>
from django.db import models
File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/utils/functional.py", line 184, in inner
self._setup()
File "/python27_runtime/python27_lib/versions/third_party/django-1.4/django/conf/__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
why is the DJANGO_SETTINGS_MODULE still undefined
Thanks
Dan
https://developers.google.com/appengine/docs/python/tools/libraries27#django
You can also try add
env_variables:
DJANGO_SETTINGS_MODULE: 'myapp.settings'
in app.yaml
Try this instead:
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'
I tried to execute
from django.db import models
but it gave the following error.. :(
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.6/site-packages/Django-1.2.2-py2.6.egg/django/db/__init__.py",
line 14, in <module>
if not settings.DATABASES:
File "/Library/Python/2.6/site-packages/Django-1.2.2-py2.6.egg/django/utils/functional.py",
line 276, in __getattr__
self._setup()
File "/Library/Python/2.6/site-packages/Django-1.2.2-py2.6.egg/django/conf/__init__.py",
line 38, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is
undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable
DJANGO_SETTINGS_MODULE is undefined.
I'm assuming you are trying to do this import from the shell/terminal? If so you need to navigate into your Django project directory and type python manage.py shell before executing your import.
You need to add DJANGO_SETTINGS_MODULE in os.environ variables to specify which your settings file is. It seems you try to use django models outside a django app. What do you try to achieve? If you just want to test with python shell, you can use the Django shell from a Django app dir:
manage.py shell --settings=settings
UPDATE: solartic was faster ;)