sebastian-ardila / google-app-engine-django

Automatically exported from code.google.com/p/google-app-engine-django
Apache License 2.0
0 stars 0 forks source link

<type 'exceptions.ImportError'>: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined #124

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I copied the new appengine_django from svn into my project.
2. I copied the new main.py from svn into my project
3. appcfg update

What is the expected output? What do you see instead?

I don't expect to see this error since __init__.py in appengine_django has
code to set this.
What I'm seeing is a strange intermittent error:
<type 'exceptions.ImportError'>: Settings cannot be imported, because
environment variable DJANGO_SETTINGS_MODULE is undefined

What version of the product are you using? On what operating system?

elsigh@kiwi:~/src/ua-profiler/appengine_django$ svn info
Path: .
URL: http://google-app-engine-django.googlecode.com/svn/trunk/appengine_django
Repository Root: http://google-app-engine-django.googlecode.com/svn
Repository UUID: d4307497-c249-0410-99bd-594fbd7e173e
Revision: 81
Node Kind: directory
Schedule: normal
Last Changed Author: mattbrown.nz
Last Changed Rev: 80
Last Changed Date: 2009-04-17 10:50:46 -0700 (Fri, 17 Apr 2009)

Please provide any additional information below.

I was able to fix this by editing main.py and adding:
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
above the first
from appengine_django import InstallAppengineHelperForDjango

Original issue reported on code.google.com by els...@gmail.com on 24 Apr 2009 at 6:33

GoogleCodeExporter commented 9 years ago
Actually, the better fix is to put this line back into the
InstallAppengineHelperForDjango routine (it used to live there) - that way other
projects (like GAEUnit) can just call this function to get up and running. 

Original comment by els...@gmail.com on 24 Apr 2009 at 7:01

GoogleCodeExporter commented 9 years ago
The line currently lives on line 128 of appengine_django/__init__.py so it will 
be
executed when appengine_django is first imported.

The only code above line 128 in __init__.py deals with finding the App Engine 
SDK,
and doesn't make any attempt to import Django at all, so I don't understand how
adding another copy of the line before the import would change the behaviour of 
the
helper in any way.

Is there anything non-standard about your project or application?

Perhaps the full traceback leading to the exception would be useful, what is 
causing
Django to be imported before line 128 of __init__.py?

Original comment by mattbrow...@gmail.com on 24 Apr 2009 at 7:16

GoogleCodeExporter commented 9 years ago
I've gotten this various times throughout my use of django on app engine, 
always interrmittently (a 
restart of the dev server or a re-upload has always fixed it). Close inspection 
always got cut off 
somewhere around app engine's approaches to caching of the methods, I have 
never run into a 
reproducible way of generating the problem.

Original comment by andyster on 24 Apr 2009 at 9:36

GoogleCodeExporter commented 9 years ago
i get something like this, consistently:

<type 'exceptions.ImportError'>: Settings cannot be imported, because 
environment
variable DJANGO_SETTINGS_MODULE is undefined.
Traceback (most recent call last):
  File "/base/data/home/apps/webjuicer/8.333642407110541968/main.py", line 31, in
<module>
    InstallAppengineHelperForDjango()
  File
"/base/data/home/apps/webjuicer/8.333642407110541968/appengine_django/__init__.p
y",
line 474, in InstallAppengineHelperForDjango
    PatchDjangoSerializationModules()
  File
"/base/data/home/apps/webjuicer/8.333642407110541968/appengine_django/__init__.p
y",
line 265, in PatchDjangoSerializationModules
    settings.SERIALIZATION_MODULES = {}
  File "/base/data/home/apps/webjuicer/8.333642407110541968/django/conf/__init__.py",
line 41, in __setattr__
    self._import_settings()
  File "/base/data/home/apps/webjuicer/8.333642407110541968/django/conf/__init__.py",
line 57, in _import_settings
    raise ImportError("Settings cannot be imported, because environment variable %s
is undefined." % ENVIRONMENT_VARIABLE)

Original comment by ionel...@gmail.com on 3 Jun 2009 at 12:37

GoogleCodeExporter commented 9 years ago
I can only fix this error by adding:
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
ALSO on line 459

Original comment by els...@gmail.com on 11 Jun 2009 at 3:36

GoogleCodeExporter commented 9 years ago
Shouldn't do that as the helpers in appengine_django fix the environ for you.

Original comment by ionel...@gmail.com on 11 Jun 2009 at 7:08

GoogleCodeExporter commented 9 years ago
Then what *should* we do about this? Having a fatal error is pretty much 
unacceptable.

Original comment by els...@gmail.com on 12 Jun 2009 at 4:22

GoogleCodeExporter commented 9 years ago
This is a funny one. I've got it at the moment.

The GoogleAppEngineLauncher runs my app OK, but when I make a request I get the 
above
error.

And yet, when I go to my app's folder and "dev_appserver.py ./" everything works
fine. This suggests that not loading the settings.py file is a symptom rather 
than
the problem. 

I tried liberally adding os.environ calls, and inserting paths onto 
sys.path.... but
it didn't work.

I have been doing a lot of work recently, and I suspect that a file has a dodgy 
char
in it or bad indentation, or writes to logging at the wrong point... but I don't
think that it's because gae can't find settings. It's summat else... to do with 
the
difference between running the nice Mac app and running it from the command 
line.

hope this helps... a bit

Original comment by remarkability@gmail.com on 13 Jun 2009 at 6:11

GoogleCodeExporter commented 9 years ago
So one thing that causes this to be a problem is when you have something which
switches context from django to webapp (especially something which uses the 
webapp
template module).
So, it seems the solution to this is to make sure that
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
is the first line of InstallAppengineHelperForDjango

Original comment by els...@gmail.com on 19 Jun 2009 at 10:02

GoogleCodeExporter commented 9 years ago
One case where I was experiencing this problem other than with gaeunit was with 
the
dev console on the task queue page

Original comment by els...@gmail.com on 19 Jun 2009 at 10:15

GoogleCodeExporter commented 9 years ago
Does os.environ work differently on appengine?

Original comment by ionel...@gmail.com on 24 Jun 2009 at 7:31

GoogleCodeExporter commented 9 years ago
No, but when the webapp.template module gets involved (which also makes use of
django's templates) it does some configuration swapping behind the scenes which
appears to sometimes possibly unset this os.environ setting. In any case, 
including
it as the first line in the InstallAppengineHelperForDjango seems to be 
fail-safe

Original comment by els...@gmail.com on 24 Jun 2009 at 2:35

GoogleCodeExporter commented 9 years ago
I still have that problem and I don't use webapp.template.

Original comment by ionel...@gmail.com on 24 Jun 2009 at 3:32

GoogleCodeExporter commented 9 years ago
Huh, I found a reference to webapp.template in my own code, and removing it 
seems to
make things work without the extra os.environ setting.
ionel.mc - Did you try my suggestion in comment #9?

Original comment by els...@gmail.com on 24 Jun 2009 at 4:36

GoogleCodeExporter commented 9 years ago
Ok I take it back, my problem has returned -  and it was indeed from context
switching from initially using something that first hits webapp.template 
(gaeunit in
my case) and then trying to hit my regular app, which uses django

Original comment by els...@gmail.com on 24 Jun 2009 at 9:38

GoogleCodeExporter commented 9 years ago
Submitted the fix I described in line 9. If anyone else gets this I guess we'll 
need
to try to figure out where the *real* problem lies.

Original comment by els...@gmail.com on 24 Jun 2009 at 9:43

GoogleCodeExporter commented 9 years ago
I'd hardly mark this as fixed - better mark it as WontFix if you want to go 
that way ...

Original comment by ionel...@gmail.com on 24 Jun 2009 at 9:48

GoogleCodeExporter commented 9 years ago
why?

Original comment by els...@gmail.com on 24 Jun 2009 at 9:50

GoogleCodeExporter commented 9 years ago
You diagnosed it wrong - the hackery in webapp.template is not the problem - 
merely a
side effect. We need to look into this more. I did mention I don't use
webapp.template at all.

Original comment by ionel...@gmail.com on 24 Jun 2009 at 9:54

GoogleCodeExporter commented 9 years ago
Ok, but does the problem still happen if you svn up with this change?
It solves the scenario where I was experiencing it. If you still have the 
problem,
let's mark this as still open.

Original comment by els...@gmail.com on 24 Jun 2009 at 10:18

GoogleCodeExporter commented 9 years ago
I'll monitor my logs these days to see how it goes - so far it was pretty
unreproductible, errors started to appear in logs a couple of days after a 
deployment.

Original comment by ionel...@gmail.com on 24 Jun 2009 at 10:24

GoogleCodeExporter commented 9 years ago
I don't think this is fixed. I'm using django 1.1 (for templates only), 
installed 
directly in my app's directory. When running in the Launcher (windows), I 
intermittently get the error about the environment variable not being set.

Curiously, I have this code at the top of each page file:

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

Settings, in turn, does this:

DEBUG = True
TEMPLATE_DIRS = ('html')

import os
os.environ["DJANGO_SETTINGS_MODULE"] = 'settings'
from google.appengine.dist import use_library
use_library('django', '1.1')
from django.template import loader

Looks like overkill, doesn't it?

I don't see how that environment variable is ever not set when django gets hit, 
yet I 
still, intermittently, get this error. I'm wondering whether os.environ[] and 
module 
load caching somehow plays into this.

Original comment by jwa...@gmail.com on 6 Jun 2010 at 7:00