Different kinds of Import Errors

Graham Dumpleton Graham.Dumpleton at gmail.com
Tue Nov 27 19:54:53 EST 2007


On Nov 28, 12:35 am, Thomas Guettler <h... at tbz-pariv.de> wrote:
> If you look at this code, you see there are two kind of ImportErrors:
>
> 1. app_name has no attribute or file managment.py: That's OK.
> 2. managment.py exists, but raises an ImportError: That's not OK: reraise
>
>         # Import the 'management' module within each installed app, to register
>         # dispatcher events.
>         for app_name in settings.INSTALLED_APPS:
>             try:
>                 __import__(app_name + '.management', {}, {}, [''])
>             except ImportError, exc:
>                 if exc.args[0]!='No module named management':
>                     raise
>
> I am searching a better solution, since in a future version of python
> the string 'No module namend management' might be changed.
>
> Any better solution?

Perhaps check for the presence of the module in sys.modules.

  if (app_name + '.management') in sys.modules:
    raise

If not there, module couldn't be found. If module there but exception
occurred then some other import related error occurred.

Graham



More information about the Python-list mailing list