Opened 13 years ago

Closed 13 years ago

Last modified 12 years ago

#16136 closed Uncategorized (worksforme)

Error was: cannot import name utils

Reported by: kurvenschubser@… Owned by: nobody
Component: Uncategorized Version: 1.3
Severity: Normal Keywords:
Cc: ognajd@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Aymeric Augustin)

Traceback (most recent call last):
   File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
     response = self.get_response(request)
   File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 141, in get_response
     return self.handle_uncaught_exception(request, resolver, sys.exc_info())
   File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py", line 165, in handle_uncaught_exception
     return debug.technical_500_response(request, *exc_info)
   File "/usr/lib/python2.5/site-packages/django/views/debug.py", line 58, in technical_500_response
     html = reporter.get_traceback_html()
   File "/usr/lib/python2.5/site-packages/django/views/debug.py", line 137, in get_traceback_html
     return t.render(c)
   File "/usr/lib/python2.5/site-packages/django/template/__init__.py", line 173, in render
     return self._render(context)
   File "/usr/lib/python2.5/site-packages/django/template/__init__.py", line 167, in _render
     return self.nodelist.render(context)
   File "/usr/lib/python2.5/site-packages/django/template/__init__.py", line 796, in render
     bits.append(self.render_node(node, context))
   File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 72, in render_node
     result = node.render(context)
   File "/usr/lib/python2.5/site-packages/django/template/debug.py", line 89, in render
     output = self.filter_expression.resolve(context)
   File "/usr/lib/python2.5/site-packages/django/template/__init__.py", line 579, in resolve
     new_obj = func(obj, *arg_vals)
   File "/usr/lib/python2.5/site-packages/django/template/defaultfilters.py", line 697, in date
     return format(value, arg)
   File "/usr/lib/python2.5/site-packages/django/utils/dateformat.py", line 281, in format
     return df.format(format_string)
   File "/usr/lib/python2.5/site-packages/django/utils/dateformat.py", line 30, in format
     pieces.append(force_unicode(getattr(self, piece)()))
   File "/usr/lib/python2.5/site-packages/django/utils/dateformat.py", line 187, in r
     return self.format('D, j M Y H:i:s O')
   File "/usr/lib/python2.5/site-packages/django/utils/dateformat.py", line 30, in format
     pieces.append(force_unicode(getattr(self, piece)()))
   File "/usr/lib/python2.5/site-packages/django/utils/encoding.py", line 66, in force_unicode
     s = unicode(s)
   File "/usr/lib/python2.5/site-packages/django/utils/functional.py", line 206, in __unicode_cast
     return self.__func(*self.__args, **self.__kw)
   File "/usr/lib/python2.5/site-packages/django/utils/translation/__init__.py", line 55, in ugettext
     return real_ugettext(message)
   File "/usr/lib/python2.5/site-packages/django/utils/functional.py", line 55, in _curried
     return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
   File "/usr/lib/python2.5/site-packages/django/utils/translation/__init__.py", line 36, in delayed_loader
     return getattr(trans, real_name)(*args, **kwargs)
   File "/usr/lib/python2.5/site-packages/django/utils/translation/trans_real.py", line 276, in ugettext
     return do_translate(message, 'ugettext')
   File "/usr/lib/python2.5/site-packages/django/utils/translation/trans_real.py", line 266, in do_translate
     _default = translation(settings.LANGUAGE_CODE)
   File "/usr/lib/python2.5/site-packages/django/utils/translation/trans_real.py", line 176, in translation
     default_translation = _fetch(settings.LANGUAGE_CODE)
   File "/usr/lib/python2.5/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
     app = import_module(appname)
   File "/usr/lib/python2.5/site-packages/django/utils/importlib.py", line 35, in import_module
     __import__(name)
   File "/usr/lib/python2.5/site-packages/django/contrib/admin/__init__.py", line 1, in <module>
     from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
   File "/usr/lib/python2.5/site-packages/django/contrib/admin/helpers.py", line 1, in <module>
     from django import forms
   File "/usr/lib/python2.5/site-packages/django/forms/__init__.py", line 17, in <module>
     from models import *
   File "/usr/lib/python2.5/site-packages/django/forms/models.py", line 6, in <module>
     from django.db import connections
   File "/usr/lib/python2.5/site-packages/django/db/__init__.py", line 77, in <module>
     connection = connections[DEFAULT_DB_ALIAS]
   File "/usr/lib/python2.5/site-packages/django/db/utils.py", line 91, in __getitem__
     backend = load_backend(db['ENGINE'])
   File "/usr/lib/python2.5/site-packages/django/db/utils.py", line 49, in load_backend
     raise ImproperlyConfigured(error_msg)
 TemplateSyntaxError: Caught ImproperlyConfigured while rendering: 'django.db.backends.mysql' isn't an available database backend. 
 Try using django.db.backends.XXX, where XXX is one of:
     'dummy', 'mysql', 'oracle', 'postgresql', 'postgresql_psycopg2', 'sqlite3'
 Error was: cannot import name utils

Change History (16)

comment:1 by anonymous, 13 years ago

This happens right on startup on a shared hosting provider. I am finding the traceback message unhelpful, since I am using "django.db.backends.mysql" in the settings.py and MySQLdb is definitely installed.

comment:2 by Luke Plant, 13 years ago

Resolution: worksforme
Status: newclosed
Type: UncategorizedBug

It works for me, and there really isn't enough information to work out why it this is happening. The only thing I can suggest is that Django has not been installed properly or fully. Clearly the 'mysql' directory exists, otherwise you would not get that error message, but it must be broken in some way, and this might not be anything to do with the code that we released.

Please re-open if you can provide enough information for us to work out why this is happening, and it is a bug in Django. A command like this might help:

DJANGO_SETTINGS_MODULE='your_settings.py' python2.5 -c 'import django.db.backends.mysql.base'

comment:3 by kurvenschubser@…, 13 years ago

Resolution: worksforme
Status: closedreopened
Type: BugUncategorized

Thanks for the feedback. Here is what I am getting now. The code you suggested is in *deployment.wsgi* towards the end.

In deployment.wsgi:

#!/usr/bin/python
import os
import sys

LIB_ROOT = "/var/www/userXXX/files/"

PROJECT_PKG = "myproject-0.2"
PROJECT_SETTINGS = 'myproject.settings'

DJANGO_PATH = "/usr/share/django/Django-1.3"

MESSAGES_PKG = "django_messages-0.4.4"
REGISTRATION_PKG = "django_registration-0.7"
TAGGING_PKG = "django_tagging-0.3.1"
MARKDOWN_PKG = "Markdown-2.0.3"
PYGMENTS_PKG = "Pygments-1.4"

PACKAGES = dict((
	## Comment this out to use the latest stable
	("django", DJANGO_PATH),
	("myproject", os.path.join(LIB_ROOT, PROJECT_PKG)),
	("messages", os.path.join(LIB_ROOT, MESSAGES_PKG)),
	("registration", os.path.join(LIB_ROOT, REGISTRATION_PKG)),
	("tagging", os.path.join(LIB_ROOT, TAGGING_PKG)),
	("markdown", os.path.join(LIB_ROOT, MARKDOWN_PKG)),
	("pygments", os.path.join(LIB_ROOT, PYGMENTS_PKG)),
))

if "django" in PACKAGES:
	path = PACKAGES.pop("django")
	sys.path.insert(0, path)

for name, path in PACKAGES.items():
	try:
		__import__(name, globals())
	except ImportError, e:
		if not path in sys.path:
			sys.path.insert(0, path)


for name, ignore in PACKAGES.items():
	try:
		__import__(name, globals())
	except ImportError, e:
		raise

os.environ['DJANGO_SETTINGS_MODULE'] = PROJECT_SETTINGS


import django.db.backends.mysql.base


from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

In error_log:

   File "/var/www/mars98/html/kurvenschubser/deployment.wsgi", line 61, in <module>
     import django.db.backends.mysql.base
   File "/usr/share/django/Django-1.3/django/db/__init__.py", line 78, in <module>
     connection = connections[DEFAULT_DB_ALIAS]
   File "/usr/share/django/Django-1.3/django/db/utils.py", line 93, in __getitem__
     backend = load_backend(db['ENGINE'])
   File "/usr/share/django/Django-1.3/django/db/utils.py", line 33, in load_backend
     return import_module('.base', backend_name)
   File "/usr/share/django/Django-1.3/django/utils/importlib.py", line 35, in import_module
     __import__(name)
   File "/usr/share/django/Django-1.3/django/db/backends/mysql/base.py", line 28, in <module>
     from django.db import utils
 ImportError: cannot import name utils

comment:4 by kurvenschubser@…, 13 years ago

looks like a circular import, I guess.
I tried versions 1.2.5 and 1.1.1 as well, it's the same issue.

comment:5 by Ramiro Morales, 13 years ago

See also #14013, #12730.

comment:6 by gloriajw, 13 years ago

I found that this error occurred for me when I migrated files from another server, but forgot to change template path. I fixed the path, and the error stopped. I think a munged path in the settings file causes this.

comment:7 by Aymeric Augustin, 13 years ago

Description: modified (diff)

When importing django.db.backends.mysql.base:

  • django/db/__init__.py executes successfully
  • django/db/__init__.py:
    • imports ConnectionHandler from django.db.utils
      • at this point the utils module from django.db is imported and working.
    • runs connections = ConnectionHandler(settings.DATABASES)
    • reaches connection = connections[DEFAULT_DB_ALIAS] which appears in the traceback above.
  • at the bottom the traceback, django/db/backends/mysql/base.py fails to import utils from django.db.

This is just weird.

Did you try to contact your hosting provider's technical support? Since it's a shared host, I guess you're using a version of Django installed by them at the system level. You must not be the only one trying to use Django with MySQL.

in reply to:  6 comment:8 by anonymous, 13 years ago

Replying to gloriajw:

I found that this error occurred for me when I migrated files from another server, but forgot to change template path. I fixed the path, and the error stopped. I think a munged path in the settings file causes this.

I take this back. This circular import happens all of the time, except when using sqlite.

comment:9 by Aymeric Augustin, 13 years ago

kurvenschubser, I just had a discussion with ramiro on IRC. To understand better your problem, here is what we need.

1) Can you run the same project locally:

  • with runserver
  • with Apache + mod_wsgi

and with MySQL. If this works, it means that the problem is triggered your hosting provider's setup.

2) Could you post your settings file? If there is a circular import loop, it's probably triggered by your settings.

Thanks.

comment:10 by kurvenschubser@…, 13 years ago

Hi aaugustin,
re. 1:
Yes I can. No problem whatsoever.
re. 2:
Here is the settings.py. I have triple checked to make sure all values from my *info files (used in function "getinfo") are correct:

import os
import logging
from logging import config
 

def getinfo(filename):
	return tuple(s.strip() for s in open(filename).next().strip(
							).split("\t"))

#absolute path to this project
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
LOGGING_CONFIG_FILE = os.path.join(PROJECT_ROOT, "logging.cfg")
 
try:
	config.fileConfig(LOGGING_CONFIG_FILE)
except:
	#rewrite logging config file
	LOGGING_FILE = open(LOGGING_CONFIG_FILE, "w")
	LOGGING_TARGET = os.path.join(PROJECT_ROOT, "django.log")
	LOGGING_CONFIG = """
[loggers]
keys=root

[handlers]
keys=hand01,hand02 

[formatters]
keys=form01 

[logger_root]
level=DEBUG
handlers=hand02 

[handler_hand01]
class=StreamHandler
level=DEBUG
formatter=form01
args=(sys.stdout,)

[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form01
args=("%(logging_target)s", "a")
 
[formatter_form01]
format= %%(levelname)s %%(pathname)s %%(lineno)d %%(asctime)s %%(message)s
datefmt=
class=logging.Formatter
""" % {"logging_target": LOGGING_TARGET}

	LOGGING_FILE.write(LOGGING_CONFIG)
	LOGGING_FILE.close()
	try:
		config.fileConfig(LOGGING_CONFIG_FILE)
	except:
		Exception("Something's wrong with the config file: '%s'" % LOGGING_CONFIG_FILE)
logging.debug("start logging")


DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (getinfo(os.path.join(PROJECT_ROOT, "admininfo")),
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS
DATABASES = {
	"default": dict(zip(("ENGINE", "NAME", "USER", "PASSWORD", "HOST", "PORT"),
				getinfo(os.path.join(PROJECT_ROOT, "dbinfo"))))
}


# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/Berlin'

# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'de-de'

SITE_ID = 2

# The directory name under which the project is saved
PROJECT_NAME = "kurvenschubser"
SERVER_ROOT = "/var/www/mars98"

# No trailing slashes, please.
PROJECT_URL = "http://www.%s.de" % (PROJECT_NAME,)

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = "%s/html/%s/static/" % (SERVER_ROOT, PROJECT_NAME)

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = "http://static.%s.de/" % (PROJECT_NAME,)


# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/django_admin_media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = getinfo(os.path.join(PROJECT_ROOT, "secretkey"))[0]

# Users section
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/accounts/login/confirm/'

# Registration section
ACCOUNT_ACTIVATION_DAYS, DEFAULT_FROM_EMAIL, EMAIL_HOST, EMAIL_HOST_USER, \
	EMAIL_HOST_PASSWORD, EMAIL_SUBJECT_PREFIX = getinfo(
			os.path.join(PROJECT_ROOT, "registrationinfo"))
ACCOUNT_ACTIVATION_DAYS = int(ACCOUNT_ACTIVATION_DAYS)
EMAIL_SUBJECT_PREFIX = EMAIL_SUBJECT_PREFIX % (PROJECT_NAME,)


# Profile section
AUTH_PROFILE_MODULE = "user_profile.UserProfile"

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    #'kurvenschubser.utils.profiling.ProfileMiddleware'
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
#    'django.core.context_processors.request',
    '%s.utils.context_processors.path' % (PROJECT_NAME,)
)

ROOT_URLCONF = '%s.urls' % (PROJECT_NAME,)



TEMPLATE_DIR_ROOT = os.path.join(PROJECT_ROOT, "templates")
TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    TEMPLATE_DIR_ROOT,
    os.path.join(TEMPLATE_DIR_ROOT, "image_manager/thumbs"),
)


INSTALLED_APPS = (
    'django.contrib.sessions',
    'django.contrib.auth',
    'django.contrib.humanize',
    'django.contrib.contenttypes',
    'django.contrib.sites',
    'tagging',
    'registration',
    'messages',
    '%s.blog' % (PROJECT_NAME,),
    '%s.image_manager' % (PROJECT_NAME,),
    '%s.user_profile' % (PROJECT_NAME,),
    '%s.site_messages' % (PROJECT_NAME,),
    'django.contrib.admin',
    'django.contrib.markup',
    'django.contrib.comments',

)

I have not yet checked with the hoster, but am going to tomorrow.

comment:11 by Aymeric Augustin, 13 years ago

I don't see anything in your settings that could trigger a circular import.

There's something unexpected that happens only on your production server.

The two tickets Ramiro linked to above were never fully understood. It'd be nice to nail down the root cause of this problem.

comment:12 by anonymous, 13 years ago

I just got a call from the sysadmin. They checked the settings.py and some other files, reset the server but to no avail. Tomorrow they are gonna upgrade to Py26. I am not holding my breath though, because I tried running locally with Py26 and Py25 (which they are using now) and they both worked equally well.

in reply to:  12 comment:13 by Ramiro Morales, 13 years ago

Replying to anonymous:

I just got a call from the sysadmin. They checked the settings.py and some other files, reset the server but to no avail. Tomorrow they are gonna upgrade to Py26. I am not holding my breath though, because I tried running locally with Py26 and Py25 (which they are using now) and they both worked equally well.

Can you provide us with details of the mod_wsgi setups (embedded mode v. daemon mode, etc.) both in your shared hosting provider and in your local test environment?

comment:14 by Ramiro Morales, 13 years ago

I suspect this is strongly related:

http://groups.google.com/group/django-users/browse_thread/thread/c354aa000fcbc87a?hl=en

i.e. the cause of this breakage is a Python web stack setup problem in the hosting provider.

comment:15 by kurvenschubser@…, 13 years ago

Resolution: worksforme
Status: reopenedclosed

Now that Py26 is being used, everything works as it should. They did an upgrade to Debian Squeeze. Thanks everybody for your valuable input.

comment:16 by Daniel Sokolowski, 12 years ago

Cc: ognajd@… added
UI/UX: unset

I have run into this problem today when taking over an existing project. The site was developed against Django 1.1, There is no 1.1 through PIP so I attempted to set it up against 1.3.3 but kept getting this error. After hours of trying to resolve this and as a last ditch effort I installed Django 1.2.7 and to my surprise the error went away. I have added myself to the CC list and so will assist further if needed. However I believe the work around is enough to leave this ticket closed as this project's does utilize an overly complicated settings approach which my gut tells me is the cause.

(virtualenv)root@klisrv03:trunk>$  ipython -i -- manage.py runserver --settings=settings_development
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40)
Type "copyright", "credits" or "license" for more information.

IPython 0.13.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/django/db/__init__.py:19: DeprecationWarning: settings.DATABASE_*                                                                                 is deprecated; use settings.DATABASES instead.
  DeprecationWarning
/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/django/db/__init__.py:60: DeprecationWarning: Short names for ENG                                                                                INE in database configurations are deprecated. Prepend default.ENGINE with 'django.db.backends.'
  DeprecationWarning
Error: cannot import name utils
An exception has occurred, use %tb to see the full traceback.

SystemExit: 1


In [1]: %tb
---------------------------------------------------------------------------
SystemExit                                Traceback (most recent call last)
/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/IPython/utils/py3compat.pyc in execfile(fname, *where)
    176             else:
    177                 filename = fname
--> 178             __builtin__.execfile(filename, *where)

/srv/www/django/<site-name>/trunk/manage.py in <module>()
      9
     10 if __name__ == "__main__":
---> 11     execute_manager(settings)

/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/django/core/management/__init__.pyc in execute_manager(settings_m                                                                                od, argv)
    436     setup_environ(settings_mod)
    437     utility = ManagementUtility(argv)
--> 438     utility.execute()

/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/django/core/management/__init__.pyc in execute(self)
    377             sys.stderr.write(self.main_help_text() + '\n')
    378         else:
--> 379             self.fetch_command(subcommand).run_from_argv(self.argv)
    380
    381 def setup_environ(settings_mod, original_settings_path=None):

/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/django/core/management/base.pyc in run_from_argv(self, argv)
    189         options, args = parser.parse_args(argv[2:])
    190         handle_default_options(options)
--> 191         self.execute(*args, **options.__dict__)
    192
    193     def execute(self, *args, **options):

/srv/www/django/<site-name>/trunk/virtualenv/lib/python2.6/site-packages/django/core/management/base.pyc in execute(self, *args, **options                                                                                )
    212                 # raise the error and quit.
    213                 sys.stderr.write(smart_str(self.style.ERROR('Error: %s\n' % e)))
--> 214                 sys.exit(1)
    215         try:
    216             self.stdout = options.get('stdout', sys.stdout)

SystemExit: 1

Note: See TracTickets for help on using tickets.
Back to Top