Skip to main content

zimport is used to load and manage python packages from zip-archives.

Project description

zimport

zimport is a drop-in replacement and enhancement for Python’s standard zipimport, with support for dynamic libraries.

zimport is used to load and manage python packages from zip-archives. in other words, it allows you to manage python packages like java jars. and, it supports not only reading files inside zip-archive, but also dynamic library(.dll, .pyd, .so) loading.

zimport has been tested on python v3.8 to v3.12, on windows/linux. also, when I tested it on various versions of pytorch, pandas, onnx, scipy, and scikitrun, it worked without any problems.

Additionally, you can create the most convenient environment possible when using it with PortablePython at the link below.
https://github.com/waveware4ai/PortablePython

If Users find bugs or problems while using the library, please leave an Issue.

History

2025/05/21 v0.1.0 : initial released
2025/05/26 v0.1.1 : support macosx, some minor bug fix

Installation (pip install)

  1. using pip
python -m pip install zimport
Collecting zimport
  Downloading zimport-0.1.0-py3-none-any.whl.metadata (5.5 kB)
Downloading zimport-0.1.0-py3-none-any.whl (32 kB)
Installing collected packages: zimport
Successfully installed zimport-0.1.0
  1. run python console then, type 'import zimport'
X:\portable.python.v3.11.09.x64.win>python
>>> import zimport
[INF] zimport installed ...
[INF] zimport cache_dir ::: [X:/portable.python.v3.11.09.x64.win/.cache] from find('.cache')
>>> zimport.__version__
'0.1'
>>>

Installation (manual install)

  1. copy zimport.zip to [python excutable path]/lib
  2. append [python excutable path]/python3xx.pth (for python 3.11 python311.pth)
./lib/site-packages.zimport.v0.1.zip
  1. run python console then, type 'import zimport'
X:\portable.python.v3.11.09.x64.win>python
>>> import zimport
[INF] zimport installed ...
[INF] zimport cache_dir ::: [X:/portable.python.v3.11.09.x64.win/.cache] from find('.cache')
>>> zimport.__version__
'0.1'
>>>

Example (read resource in zip)

  1. First, paste [ReadResource] folder inside [examples/exam.01] into [lib/site-package] folder.
  2. __init__.py inside ReadResource has simple contents as follows.
import sys, io, os
def read() :
    path = os.path.dirname(os.path.abspath(__file__))
    #print(path)
    f = open(os.path.join(path, "config.ini"), 'r')
    while True:
        line = f.readline()
        if not line: break
        print(line.strip())
    f.close()
  1. If you run the code, you will see the following result:
X:\portable.python.v3.11.09.x64.win>python
>>> import ReadResource
>>> ReadResource.read()
[1] This
[2] is a
[3] resource
[4] inner
[5] package
>>> 
  1. Next, compress the [ReadResource] folder to ../site-packages.ReadResource.z and delete the [ReadResource] folder.
  2. If you run python and do the same, you can see that it cannot read internal resources.
X:\portable.python.v3.11.09.x64.win>python
>>> import os, sys
>>> sys.path.append(os.path.join(os.getcwd(), "lib/site-packages.ReadResource.z"))
>>> import ReadResource
>>> ReadResource.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "X:\portable.python.v3.11.09.x64.win\lib\site-packages.ReadResource.z\ReadResource\__init__.py", line 6, in read
FileNotFoundError: [Errno 2] No such file or directory: 'X:\\portable.python.v3.11.09.x64.win\\lib\\site-packages.ReadResource.z\\ReadResource\\config.ini'
>>>
  1. zimport was developed to allow normal reading of internal resources.
X:\!test\portable.python.v3.11.09.x64.win>python
>>> import zimport
[INF] zimport installed ...
[INF] zimport cache_dir ::: [X:/!test/portable.python.v3.11.09.x64.win/.cache] from find('.cache')
>>> import os, sys
>>> sys.path.append(os.path.join(os.getcwd(), "lib/site-packages.ReadResource.z"))
>>> import ReadResource
>>> ReadResource.read()
[1] This
[2] is a
[3] resource
[4] inner
[5] package

Example (build pytorch zip-archive)

  1. delete or move all files in [python excutable path]/lib/site-packages to another location. (This is because files stored in site-packages must be deleted after creating the zip archive.)
  2. download torch and dependencies via pip. Here we will use torch version 2.3.1.
python -m pip install torch==2.3.1 torchvision==0.18.1 torchaudio==2.3.1 --index-url https://download.pytorch.org/whl/cu118
  1. Once the download is complete, check that the package is working properly.
python -c "import torch;print(torch.__version__);print(torch.cuda.is_available());print(torch.cuda.get_device_name(0));"
2.3.1+cu118
True
NVIDIA GeForce RTX 2080
  1. Now go into site-packages and zip-archive all the files and empty site-packages.
    When compressing, you can compress it with the extension [.zip] or [.z], Personally, I prefer [.z] because Windows systems recognize [.zip] as a zip folder and perform caching. If there are a lot of files inside [.zip], this may cause unnecessary overhead or system crashes on Windows systems.
cd [python excutable path]/lib/site-packages
zip -r ../site-packages.torch.v2.3.1+cu118.win.zip *
rm -rf *
  1. There are two ways to register sys.path for loading zip-archive. The first way is to register in python3xx.pth. Add the content as follows.
./lib/site-packages.torch.v2.3.1+cu118.win.zip

The second method is to add dynamically. Add it to sys.path in python colsole as shown below.

import os, sys
sys.path.append(os.path.join(os.getcwd(), "lib/site-packages.torch.v2.3.1+cu118.win.zip"))

You can check that both are registered through sys.path.

>>> for i in range(len(sys.path)) : print(f"[{i}] : {sys.path[i]}")
[0] : X:\python\portable.python.v3.11.09.x64.win
[1] : X:\python\portable.python.v3.11.09.x64.win\python311.zip
[2] : X:\python\portable.python.v3.11.09.x64.win\DLLs
[3] : X:\python\portable.python.v3.11.09.x64.win\Lib
[4] : X:\python\portable.python.v3.11.09.x64.win\lib\site-packages
[5] : X:\python\portable.python.v3.11.09.x64.win\lib\site-packages.pip.zip
[6] : X:\python\portable.python.v3.11.09.x64.win\lib\site-packages.zimport.v0.1.zip
[7] : X:\python\portable.python.v3.11.09.x64.win\lib/site-packages.torch.v2.3.1+cu118.win.zip
  1. The final step is to import the pytorch zip-archive module.
>>> import zimport, os, sys
[INF] zimport installed ...
[INF] zimport cache_dir ::: [X:/python/portable.python.v3.11.09.x64.win/.cache] from find('.cache')
>>> sys.path.append(os.path.join(os.getcwd(), "lib/site-packages.torch.v2.3.1+cu118.win.zip"))
>>> import torch
>>> print(torch.__version__)
2.3.1+cu118
>>> print(torch.cuda.is_available())
True
>>> print(torch.cuda.get_device_name(0))
NVIDIA GeForce RTX 2080
>>>

If you added path to python3xx.pth, you can call it elegantly in one line.

python -c "import zimport;import torch;print(torch.__version__);print(torch.cuda.is_available());print(torch.cuda.get_device_name(0));"
2.3.1+cu118
True
NVIDIA GeForce RTX 2080
>>>

Usage

zimport.install()                # Since it is installed at the same time as import, there is no need to use it in general cases.
zimport.debug(True/False)        # Used to perform debugging. The default is False.
zimport.invalidate_caches()      # Used to clear cache information. Mainly needed for reloading when modifying .py.
zimport.precache_dll(PATH)       # This pre-caches the dll. Normally you won't need to do this.
zimport.precache_file(PATH)      # This pre-caches the file. Normally you won't need to do this.
zimport.precache_directory(PATH) # This pre-caches the directory. Normally you won't need to do this.

Save your Storage

torch before after

Project details


Download files

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

Source Distribution

zimport-0.1.2.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zimport-0.1.2-py3-none-any.whl (26.0 kB view details)

Uploaded Python 3

File details

Details for the file zimport-0.1.2.tar.gz.

File metadata

  • Download URL: zimport-0.1.2.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for zimport-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f6e89b72e11b182d2a68ef422f1660980d9f750955f3929b3bb7f6395058841f
MD5 9aa2f6d0300f755c7d39e73b449b8509
BLAKE2b-256 64b2044f84dab2333f9f6020bea3847faff0b10bf25ef2abdd8bbab96145b296

See more details on using hashes here.

File details

Details for the file zimport-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: zimport-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 26.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for zimport-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6f1b3b2fb5b111e1ad85cef6c3be42e95062018245eaf011c436d5cb105995dc
MD5 6a7941f65e0c28e5f27c21fa17a3cff3
BLAKE2b-256 ec65d77840656f62c55378ac00d217a3dae18425b4f6a3f10501bea3a1587974

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page