Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python import modules from Zip archives (zipimport)
The zipimport module allows Python to import modules and packages directly from ZIP archives. This feature is useful for distributing Python applications as single ZIP files or organizing multiple modules in a compressed format.
Creating a ZIP Archive
First, let's create a ZIP archive containing Python modules. We'll use the zipfile module to compress multiple Python files ?
import zipfile
import os
# Create some sample Python files first
with open('hello.py', 'w') as f:
f.write('def greet(name):\n return f"Hello, {name}!"\n\nprint(greet("World"))')
with open('math_utils.py', 'w') as f:
f.write('def add(a, b):\n return a + b\n\ndef multiply(a, b):\n return a * b')
# Create ZIP archive
zf = zipfile.PyZipFile('modules.zip', mode='w')
files = ['hello.py', 'math_utils.py']
for file in files:
if os.path.exists(file):
zf.write(file)
print(f"Added {file} to archive")
zf.close()
print("ZIP archive 'modules.zip' created successfully")
Added hello.py to archive Added math_utils.py to archive ZIP archive 'modules.zip' created successfully
Using zipimporter Class
The zipimport module provides the zipimporter class with several methods for working with ZIP archives ?
Creating a zipimporter Instance
import zipimport
# Create zipimporter instance
importer = zipimport.zipimporter('modules.zip')
print(f"Zipimporter created for: {importer}")
Zipimporter created for: <zipimporter object "modules.zip">
Finding Modules with find_module()
The find_module() method searches for a specified module and returns the zipimporter instance if found, or None if not found ?
import zipimport
importer = zipimport.zipimporter('modules.zip')
# Search for existing module
result = importer.find_module('hello')
print(f"Found 'hello' module: {result}")
# Search for non-existing module
result = importer.find_module('nonexistent')
print(f"Found 'nonexistent' module: {result}")
Found 'hello' module: <zipimporter object "modules.zip"> Found 'nonexistent' module: None
Loading Modules with load_module()
The load_module() method loads and returns the specified module ?
import zipimport
importer = zipimport.zipimporter('modules.zip')
# Load module from ZIP
math_module = importer.load_module('math_utils')
print(f"Module name: {math_module.__name__}")
print(f"Module file: {math_module.__file__}")
print(f"Add function result: {math_module.add(5, 3)}")
Module name: math_utils Module file: modules.zip/math_utils.py Add function result: 8
Getting Source Code with get_source()
The get_source() method returns the source code of a module as a string ?
import zipimport
importer = zipimport.zipimporter('modules.zip')
# Get source code
source = importer.get_source('math_utils')
print("Source code of math_utils:")
print(source)
Source code of math_utils:
def add(a, b):
return a + b
def multiply(a, b):
return a * b
Adding ZIP Files to sys.path
You can add ZIP archives directly to sys.path to import modules normally ?
import sys
# Add ZIP file to Python path
sys.path.insert(0, 'modules.zip')
# Now import normally
import math_utils
print(f"Using imported module: {math_utils.multiply(4, 7)}")
Using imported module: 28
zipimporter Methods Summary
| Method | Purpose | Return Value |
|---|---|---|
find_module(name) |
Search for a module | zipimporter instance or None |
load_module(name) |
Load and return module | Module object |
get_source(name) |
Get module source code | Source code string |
get_code(name) |
Get compiled code object | Code object |
Conclusion
The zipimport module provides a powerful way to import Python modules from ZIP archives. This is particularly useful for distributing applications as single files or organizing related modules in a compressed format.
