March 30, 2009 will

Misleading ImportErrors in Django

I was debugging an issue with our Django app at work today; an admin.py file wasn't being picked up, and nothing was appearing in the admin pages. It turned that an ImportError was being thrown in the admin.py and Django was interpreting this as the file not existing.

I'm assuming that the reason for this is that Django uses __import__ to import the module, and catches ImportError's if the admin.py doesn't exist. The trouble with this is that if admin.py does exist, and throws an ImportError of its own, Django will also interpret that as a missing admin.py – which can be misleading.

The only solution I can think of to more accurately handle this would be to programmaticaly examine the traceback to determine where the ImportError is thrown. If the traceback is one level deep, we know that the ImportError was thrown because the module doesn't exists. If it is greater than one level then we know the module was found, but has thrown an ImportError of its own.

Here's a simple proof of concept:

#check_import.py
import traceback
import sys

def check_import(name):
    try:
        __import__(name)
    except ImportError:
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        return len(traceback.extract_tb(exceptionTraceback)) > 1
    return True

print check_import("os")
print check_import("noimport")
print check_import("anotherlevel")
#anotherlevel.py
import thismoduledoesnotexist

This produces the following when run:

True
False
True

It would be nice if Django did something similar for its implicit imports. I think the best behaviour would be re-raise the ImportError if it the module does actually exist. That way, it is clear what the problem is. I may attempt to write a patch at some point, unless someone knows of a better solution.

Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
gravatar
Ramiro Morales
What version/revision of Django are you using?. Since r10088 __import__ isn't used anymore in Django trunk (Brett Cannon's importlib is now used and included in Django utils library) together with the imp module.

See http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/__init__.py#L40
gravatar
WIll McGugan
An earlier version that that it seems. Problem solved! :-)
gravatar
robert marsanyi
Wouldn't you want to have Django throw something subclassed from ImportException, so you could tell the difference?
gravatar
rates
yes, django sometimes throws really wired error
gravatar
暴力摩托下载
Wouldn't you want to have Django throw something subclassed from ImportException