Skip to main content

This package aims to speed up imports from zipfiles, by pre-computing the “directory information” dictionary and storing it in a separate index file. This reduces the time spent parsing information out of the zipfile.

It exports a single useful name, zipimporter, which is a drop-in replacement for the standard zipimporter class.

To create an index for a given zipfile, do the following:

from zipimportx import zipimporter
zipimporter("mylib.zip").write_index()

Depending on your platform, this will create either “mylib.zip.win32.idx” or “mylib.zip.posix.idx” containing the pre-parsed zipfile directory information. (Specifically, it will contain a marshalled dictionary similar to those found in zipimport._zip_directory_cache.)

To enable use of these index files, simply replace the builtin zipimport mechanism with zipimportx by doing the following:

import zipimportx
zipimportx.zipimporter.install()

In my tests, use of these indexes speeds up the initial loading of a zipfile by about a factor of 3 on Linux, and a factor of 5 on Windows.

Note that this package uses nothing but builtin modules. To bootstrap zipfile imports for a frozen application, you can inline the module’s code directly into your application’s startup script. Do this somewhere in your build:

import zipimportx
import inspect

SCRIPT = '''
%s
zipimporter.install()
import myapp
myapp.main()
''' % (inspect.getsource(zipimportx),)

freeze_this_script_somehow(SCRIPT)
zipimportx.zipimporter("path/to/frozen/library.zip").write_indexes()

Note also that imports will almost certainly break if the index does not reflect the actual contents of the zipfile. This module is therefore most useful for frozen apps and other situations where the zipfile is not expected to change.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zipimportx-0.2.0.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file zipimportx-0.2.0.tar.gz.

File metadata

  • Download URL: zipimportx-0.2.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for zipimportx-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c5d2e4c40f5cb11af53a40491371d8b525fb58c630cc7bfb1ac1e1ff06c43158
MD5 e0429b7a0a359199be22fa322000f040
BLAKE2b-256 8140c366da0bd28bc589af6c2008f7c981a1d4865ba2d1cbe0668c489570ca47

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.2

1 file

0.3.1

1 file

0.3.0

1 file

This release

0.2.0 This release

1 file

0.1.0

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page