Different kinds of Import Errors

Thomas Guettler hv at tbz-pariv.de
Fri Nov 30 08:24:18 EST 2007


Sorry, but this does not work. If there is an ImportError
during importing the existing module, it won't get inserted
into sys.modules. I just tried it with a small example.

An other solution would be to inspect the traceback. If the
app_name+'.management' is in it, it exists.

Graham Dumpleton schrieb:
> 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