views:

91

answers:

2

Hi there,

I have a problem with Django settings. My app runs with app-engine-patch. I added a script that runs without django, and is reached directly via the app.yaml handlers. I then get this error:

File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/conf/__init__.py", line 53, in _import_settings
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMENT_VARIABLE
<type 'exceptions.EnvironmentError'>: Environment variable DJANGO_SETTINGS_MODULE is undefined.

I found this tip in google:

# 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' 

But then I got this error:

raise EnvironmentError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
<type 'exceptions.EnvironmentError'>: Could not import settings 'settings.py' (Is it on sys.path? Does it have syntax errors?): No module named ragendja.settings_pre

I think it is a problem with app-engine-patch paths modification, how can I import settings_pre correctly?

Thanks!

+2  A: 

Change

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings.py' 

to

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

The value of the DJANGO_SETTINGS_MODULE is the name of the module (ie, as you would write it in an import statement in a Python script), not the path to the module.

Will McCutchen
same error with settings and settings.py
And you've got a file called `settings.py` in the root of your App Engine project?
Will McCutchen
Yes.I think it finds the settings.py file without problem, but this file begins with from ragendja.settings_pre import *Due to app-engine-patch path modification, ragendja is not found (whereas it is imported correctly in the rest of the app).
So you'll have to do some manual modification of your `sys.path` to make the `ragendja` module available. Wherever you're doing this `os.environ` modification, make sure the `ragendja` module is in `sys.path`.
Will McCutchen
How can I do that? I'm a total newbie to this matter.
Probably should be a separate question, or a Google search, but here's the short version: `import sys; sys.path.append('path/to/parent/dir/of/ragendja/module/')`. So, say your `ragendja` module is at `appenginepatch/ragendja/` from the root of your project, you'd use something like `appenginepatch/` as the argument to `sys.path.append`.
Will McCutchen
A: 

Thanks to another question, I replaced the beinning with:

from common.appenginepatch.aecmd import setup_env 
setup_env(manage_py_env=True)

This imports all the settings and my task can run without reference to Django