module import performance question

Jeff Sykes jeff at
Tue Nov 11 20:58:36 CET 2003

Thanks, Emile, that's a good idea. I found another issue, too. See my
response to another poster below:

>> I think there was a different culprit, though. After some more 
>> dilligent logging, I realized that immediately after import I 
>> called upon an attribute of the imported module that wouldn't 
>> _necessarily_ be present. When the attribute was not present, 
>> it raised an exception, which I caught. This exception handling 
>> was a real performance anchor! I now check hasattr() before I 
>> execute this block of code, and this has helped performance 
>> significantly.
>> Here's a snip of the block that occured right after import:
>> try:
>>     # next line was not there before
>>     if hasattr(dyn_blocks[key], "BUFFER_ON"):
>>         context.setBufferFlag(dyn_blocks[key].BUFFER_ON)
>> except:
>>     Log.stacktrace(Log.ERROR)
>> I guess the lesson I learned is don't use exception handling 
>> unnecessarily. Which I know not to do anyway. Sigh. Back to 
>> school for me.

"Emile van Sebille" <emile at> wrote in message news:<boqutc$1gl3q2$1 at>...
> "Jeff Sykes" <jeff at> wrote in message
> news:729b56c7.0311102210.4869d4f6 at
> > I have a cgi script that imports modules based on a user config
>  file.
> > I am suffering some performance problems when I import these
>  modules.
> > Some logging revealed that it seems to be taking about 1.3 seconds
>  to
> > import these modules. This is running on a Windows 2000 box with a
> > Pentium II 400 processor, Python 2.1, Apache 1.3.19 as the web
>  server.
> > I don't need screaming performance numbers, but this time is
> > excessive. Is it me, or does that number seem kind of slow?
> How many modules are being imported?
> > Any code
> > optimizations or recommendations?
> Don't append to sys.path.  Try sys.path.insert(0,addlpath).  Then the
> import will get an immediate hit and always the right module rather
> than stepping through all the preceeding directories.
> HTH,
> Emile van Sebille
> emile at

More information about the Python-list mailing list