Michael Fourre
Michael Fourre

Reputation: 3015

Module 'main' has no attribute application -- Google App Engine Python with Django

I have been running into this problem for a short while now and simply can't find a solution anywhere. I am using Google App Engine to run a default Python 2.7 app with Django 1.5 (via GAE SDK) created through PyCharm. I can upload the app successfully, but upon visiting the actual page, I get a Server Error. Then, checking the logs in Google App Engine, I see this:

ImportError: <module 'main' from '/base/data/home/apps/s~eloquent-ratio-109701/1.388053784931450315/main.pyc'> has no attribute application

After searching the internet for a while, I was able to find a few posts which address this issue, but attempting them never seemed to solve my problem. For example: This problem was solved by replacing "application" with "app" in the following lines:

application = django.core.handlers.wsgi.WSGIHandler()
util.run_wsgi_app(application)

In fact, I had run into this same issue before and this solution provided a fix for me in the past, however at that time I was running a separate app and it was not through the GAE.

I checked the Django documentation for version 1.5 here, but the code and suggestions there don't seem to conflict with what I currently have in my project.

I read a bit more about this type of problem, and saw another post that suggested checking the app's wsgi.py file to ensure that it is named 'application' or 'app' respectively, so that one could then use that same name throughout the rest of the application. However, upon checking those settings I saw that 'application' was used there too:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

There's even a line in settings.py which uses the same nomenclature to declare the WSGI application:

WSGI_APPLICATION = 'Chimera.wsgi.application'

I'm really having trouble debugging this. I get the feeling it's really dumb and I just can't see it, but unfortunately I'm not particularly good at this kind of stuff -- I'm still a bit of a novice in this field.

Does anyone have any idea what I could try in an attempt to fix this issue?

UPDATE: I started making line by line changes and testing things, and eventually I found that the GAE log changes depending on the input for the "script" under app.yaml. So if I change the script under "handlers" between "main.app" and "main.application", it adjusts the log output to refer to "app" or "application" respectively. So that line in the app.yaml file tells the app what to look for, but I'm still not seeing why it can't be found. Not sure what else I could change to test it out. I wish I knew a bit more about the actual inner workings so that I could figure out why the app is confused about the attribute. Is it trying to run before it even gets instantiated or something?

Source code below:

main.py

import os, sys

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

from google.appengine.ext.webapp import util

from django.conf import settings

settings._target = None

import django.core.handlers.wsgi
import django.core.signals
import django.db
import django.dispatch.dispatcher

def main():
    application = django.core.handlers.wsgi.WSGIHandler()

    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()

app.yaml

application: eloquent-ratio-109701
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.application

libraries:
- name: django
  version: 1.5

wsgi.py

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Chimera.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Full log from GAE:

Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 302, in _LoadHandler
raise err
ImportError: <module 'main' from '/base/data/home/apps/s~eloquent-ratio-109701/1.388053784931450315/main.pyc'> has no attribute application

Thanks for helping me out.

Upvotes: 1

Views: 2291

Answers (2)

Dan Cornilescu
Dan Cornilescu

Reputation: 39824

In your main.py file (i.e. the main module) application is a variable inside the main() function, not an attribute of the main module. Basically you don't need a main() function.

GAE has some specific support for using Django, I'd strongly suggest going through the Django Support documentation and the Django App example.

Upvotes: 4

Michael Fourre
Michael Fourre

Reputation: 3015

Based on the comment made by @DanielRoseman I discovered that declaring the app inside of the main() function caused an issue because the app attribute was then only accessible at the main() function level, as it was a member variable of main() as opposed to a global variable. Although the default application files were structured this way by PyCharm, it seems that it was incorrect. I'm not sure if this is a compatibility issue, but regardless, moving the app declaration outside of the main() function adjusts the scope in a way which allows for other parts of the project to access it, solving my problem.

Thank you @DanielRoseman for the comment.

Upvotes: 1

Related Questions