Skip to content

Commit 5a3d153

Browse files
committed
fix Deprecated since 3.10 zipimporter.load_module. fix #32
1 parent e11a64f commit 5a3d153

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ So, what could `zipapps` be?
7777

7878
## 4. Activate the `.pyz` environment
7979

80-
1. one way is use `zipimport` (Recommended)
81-
1. load from pyz file
82-
1. python < 3.10: `import zipimport; zipimport.zipimporter('bottle.pyz').load_module("ensure_zipapps")`
80+
1. use `zipimport` (Recommended)
81+
1. `zipimport.zipimporter("some_lib_venv.pyz").find_spec("ensure_zipapps").loader.load_module("ensure_zipapps")`
8382
2. automatically unzip cache, and add the path to sys.path
8483
1. it can be run multiple times
8584
2. if they are all pure-python code and **no need to decompress**

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- Or `zipapps.exe ...` on Windows, or `zipapps` on Unix
1313
- Added `--uv` option to accept a path to uv, speeding up `pip install`
1414
- Added `--freeze-deps` as an alias for `--freeze-reqs`
15+
- fix Deprecated since 3.10 `zipimporter.load_module`
1516
- 2024.08.07
1617
- [**Compatible WARNING**]: update `sys_paths` insert index from `-1` to `0`
1718
- disable `--download-python`, use `python -m zipapps.download_python` instead

test_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,9 @@ def test_env_usage():
407407
_clean_paths(root=False)
408408
create_app(output="bottle_env.pyz", unzip="bottle", pip_args=["bottle"])
409409
# activate sys.path and unzip cache
410-
zipimport.zipimporter("bottle_env.pyz").load_module("ensure_zipapps")
410+
spec = zipimport.zipimporter("bottle_env.pyz").find_spec("ensure_zipapps")
411+
if spec and spec.loader:
412+
spec.loader.load_module("ensure_zipapps")
411413
import bottle
412414

413415
# using app unzip cache for `import ensure_zipapps`
@@ -762,7 +764,11 @@ def main():
762764

763765
count = 0
764766
start = 0
765-
items = [(name, func) for name, func in globals().items() if name.startswith("test_") and inspect.isfunction(func)]
767+
items = [
768+
(name, func)
769+
for name, func in globals().items()
770+
if name.startswith("test_") and inspect.isfunction(func)
771+
]
766772
name_list = ""
767773
for name, func in items:
768774
if name_list and name not in name_list:

zipapps/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ def main():
407407
default="",
408408
dest="uv_path",
409409
help="the executable path of python-uv, to speed up pip install",
410-
) # internal use only
410+
)
411411
if len(sys.argv) == 1:
412412
parser.print_help()
413413
handle_win32_embeded()

zipapps/activate_zipapps.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ def activate(path=None):
99
path_str = path.absolute().as_posix()
1010
if zipfile.is_zipfile(path_str):
1111
try:
12-
_tmp = zipimport.zipimporter(path_str).load_module("ensure_zipapps")
13-
modules.pop(_tmp.__name__, None)
14-
del _tmp
15-
return True
12+
spec = zipimport.zipimporter(path_str).find_spec("ensure_zipapps")
13+
if spec and spec.loader:
14+
_tmp = spec.loader.load_module("ensure_zipapps")
15+
modules.pop(_tmp.__name__, None)
16+
del _tmp
17+
return True
18+
else:
19+
raise ImportError(path_str)
1620
except ImportError as err:
1721
stderr.write(f"WARNING: activate failed for {err!r}\n")
1822
raise err

zipapps/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ def kwargs(self):
224224
unzip_exclude=self.unzip_exclude,
225225
chmod=self.chmod,
226226
clear_zipapps_self=self.clear_zipapps_self,
227+
uv_path=self.uv_path,
227228
)
228229

229230
def ensure_args(self):

0 commit comments

Comments
 (0)