Search code examples
djangogoogle-app-enginesqlitedjango-1.4

Django 1.4 on GAE: sqlite "ImportError: cannot import name utils"


I'm trying to get sqlite3 support working on Django 1.4 on Google App Engine 1.7.4 on Python 2.7.

I fiddled around with the "Google Cloud SQL" database backend, all worked well (syncdb, insert/update/delete, ...).

But then I enabled sqlite (as Google Cloud SQL is slow when on localhost):

import os
if (os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine') or
    os.getenv('SETTINGS_MODE') == 'pushtolive'):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'google.appengine.ext.django.backends.rdbms',
            'INSTANCE': 'xyz:xyz',
            'NAME': 'my_database',
        }
    }
else:
    # Running in development, so use a local SQLite database
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': '/tmp/mysite.db',
        }
    }

and when I open any url (e.g. 127.0.0.1:8080) then I run into this monstrous stack trace. I stripped down the stack trace so it's more readable:

ERROR    2012-12-29 09:07:06,223 base.py:215] Internal Server Error: /favicon.ico
Traceback (most recent call last):
  File "/Users/philipp/python/mysite/urls.py", line 4, in <module>
    from django.contrib import admin
  File ".../google_appengine/lib/django_1_4/django/contrib/admin/__init__.py", line 3, in <module>
    from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
  File ".../google_appengine/lib/django_1_4/django/contrib/admin/helpers.py", line 2, in <module>
    from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
  File ".../google_appengine/lib/django_1_4/django/contrib/admin/util.py", line 1, in <module>
    from django.db import models
  File ".../google_appengine/lib/django_1_4/django/db/__init__.py", line 40, in <module>
    backend = load_backend(connection.settings_dict['ENGINE'])
  File ".../google_appengine/lib/django_1_4/django/db/__init__.py", line 34, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File ".../google_appengine/lib/django_1_4/django/db/utils.py", line 92, in __getitem__
    backend = load_backend(db['ENGINE'])
  File ".../google_appengine/lib/django_1_4/django/db/utils.py", line 24, in load_backend
    return import_module('.base', backend_name)
  File ".../google_appengine/lib/django_1_4/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File ".../google_appengine/lib/django_1_4/django/db/backends/sqlite3/base.py", line 14, in <module>
    from django.db import utils
ImportError: cannot import name utils

From the stack trace I read that the execution runs to the django.db.utils module, then into django.db.backends.sqlite3.base, then it tries to jump into django.db.utils again but that then strangely fails.

The problem seems to be in the GAE environment, since this works:

python mysite/manage.py syncdb

And this as well:

python mysite/manage.py shell
>>> from django.contrib.auth.models import User
>>> u = User(username="hans")
>>> u.save()
>>> User.objects.all()
[<User: hans>]

What I tried so far:

  • switched from/to Django 1.3/1.4
  • uninstalled GAE, installed it again
  • I tried through all the solutions google reveals for "ImportError from django.db import utils"
  • there is this similar question. I tried executing the statements in the question and I don't run into the problem.

I'm on OS X 10.8.2 My PYTHONPATH is :/usr/local/google_appengine:/usr/local/google_appengine/lib/django_1_4


Solution

  • As it's not good practice to use sqlite in dev and mysql (as used by Cloud SQL) in production I don't recommend this setup to anyone.

    Then, there is the option --use_sqlite: This is used to speed up development when using the Google Datastore. There is no similar option for Cloud SQL.