Opened 12 years ago

Closed 11 years ago

#2947 closed defect (duplicate)

ImportError raised in my is swallowed

Reported by: paul@… Owned by: nobody
Component: Core (Other) Version:
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This code in "" has the same bug as was reported in ticket 2669

# Import the 'management' module within each installed app, to register
# dispatcher events.
for app_name in settings.INSTALLED_APPS:


import(app_name + '.management', , , [])

except ImportError:


If mysite/myapp/ tries to import a non-existent module, or imports a module that imports a non-existent module, then is ignored silently. This is very hard to diagnost and debug! At first, I had no idea why seemed to cease to exist. Then after a while, I guessed, based on my knowledge of how Python works.

The solution should be the same as for 2669. Perhaps it would be good to audit all situations where ImportError is raised.

Attachments (2)

management_import_patch.diff (2.0 KB) - added by paul@… 12 years ago.
Patch to fix import bug (5.7 KB) - added by paul@… 12 years ago.
Test case for fix.

Download all attachments as: .zip

Change History (6)

comment:1 Changed 12 years ago by anonymous

Maybe there should be a helper function that is used globally instead of import.

import sys
import imp

def module_exists(modulename):
        return True
    except ImportError:
        return False

def import_if_exists(modulename):
        module = __import__(modulename)
        return module
    except ImportError, e:
        if module_exists(modulename):
            return None

def test():
    # this test depends upon the existance of a "" which imports
    # some other non-existent module (or otherwise raises an ImportError)
    assert import_if_exists("xml.dom"), "xml.dom does exist"
    assert not load_if_exists("foobar"), "foobar does not exist"
        raises_import_error = load_if_exists("foo")
        assert False, "Should not have gotten here"
    except ImportError:
    print "Tests succeeded!"


Changed 12 years ago by paul@…

Patch to fix import bug

Changed 12 years ago by paul@…

Attachment: added

Test case for fix.

comment:2 Changed 12 years ago by paul@…

Note that the patch is still not quite correct. It works only if the parent of the project directory is in sys.path but I think that Django is designed to not depend upon this, right?

comment:3 Changed 12 years ago by Simon G. <dev@…>

Triage Stage: UnreviewedDesign decision needed

comment:4 Changed 11 years ago by Jacob

Resolution: duplicate
Status: newclosed

This is really a symptom of a bigger problem with Python exceptions themselves (search python-dev for discussion of chained exceptions to see the long history here). I'm wary of working around it since it seems like a lot of code; a better aproach would be to re-raise the exception using the --traceback mechanism added in #4431.

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