March 30, 2009 will

Misleading ImportErrors in Django

I was debugging an issue with our Django app at work today; an file wasn't being picked up, and nothing was appearing in the admin pages. It turned that an ImportError was being thrown in the 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 doesn't exist. The trouble with this is that if does exist, and throws an ImportError of its own, Django will also interpret that as a missing – 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:
import traceback
import sys

def check_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")
import thismoduledoesnotexist

This produces the following when run:


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](

> This is a quote

import this
your comment will be previewed here
Ramiro Morales 7 years ago
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.

WIll McGugan 7 years ago
An earlier version that that it seems. Problem solved! :-)
robert marsanyi 7 years ago
Wouldn't you want to have Django throw something subclassed from ImportException, so you could tell the difference?
rates 7 years ago
yes, django sometimes throws really wired error
暴力摩托下载 6 years ago
Wouldn't you want to have Django throw something subclassed from ImportException