how to optimize zipimport

John Machin sjmachin at
Thu Apr 2 12:26:48 CEST 2009

On Mar 30, 11:39 pm, Coonay <fla... at> wrote:
> On Mar 26, 1:38 pm, John Machin <sjmac... at> wrote:
> > On Mar 26, 2:06 pm, Coonay <fla... at> wrote:

> > > On Mar 26, 10:41 am, Coonay <fla... at> wrote:
> > > > in my mudule ,i import another mudule from a zip ,
> > > > when i call  my mudle method,the module in the zip will be import from
> > > > the zip every time,that slow down the operation,
> > > > i try to search the some workarodnd or solution but i don't get one,
> i don't use zipimport directly,just like u said i put the zip in the
> sys.path
> i found the the module in the zip reloaded  everytime the code is
> called,i mean, say ,first time it take me 1 second to call a method in
> the zip,but it take another 1 second to call the mothod for another
> time ,and so on

How do you know that it is being reloaded each time a methos is

> > Note: it may help when you reply to show a stripped-down version of
> > your calling module, sufficient to back up your description of what is
> > happening.

We can only guess if you don't show us what is actually happening.
Vague descriptions are not very useful. Show us how exactly you are
importing the modules, how you are calling methods, with timing
calls ...

By the way, have you read this part of the documentation:
"Note that if an archive only contains .py files, Python will not
attempt to modify the archive by adding the corresponding .pyc or .pyo
file, meaning that if a ZIP archive doesn't contain .pyc files,
importing may be rather slow."

Do you have .pyc files in your ZIP file? If not that would make the
first import slower than it should be.

However second, third, ... imports (if you do any) should be
instantaneous -- unless you are messing about deleting modules from

And *method* calls should be instantaneous.

Does your app run at the same speed when you have all the modules in a
directory instead of inside a zip file?

Apart from code executed once at app start to inject the path to the
zip file into sys.path, have you made any other changes to accommodate
using import from zip? If so, what changes?

Have you tried running your app from the shell (Windows translation:
in a Command Prompt window) like this:

python -v

This will show you all the imports as they happen.

HTH ... but a clear description from you (plus code samples) of
exactly what you are doing woul dhelp a whole lot more.


More information about the Python-list mailing list