Skip to content

Commit 73afba9

Browse files
committed
add test_uvx_zipapps for uvx usage
1 parent 3c8cd96 commit 73afba9

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,34 @@ So, what could `zipapps` be?
7777

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

80-
1. use `zipimport` (Recommended)
80+
1. use `importlib` (Recommended)
8181
1. `sys.path.insert(0, "some_lib_venv.pyz");importlib.import_module("ensure_zipapps")`
8282
2. automatically unzip cache, and add the path to sys.path
8383
1. it can be run multiple times
8484
2. if they are all pure-python code and **no need to decompress**
8585
1. `impory sys; sys.path.insert(0, "bottle.pyz")`
86+
3. use `zipimport`
87+
88+
```python
89+
# 1. use zipapps lib
90+
from zipapps.activate_zipapps import activate
91+
activate("app.pyz")
92+
93+
# or use source code directly
94+
from zipimport import zipimporter
95+
96+
importer = zipimporter("app.pyz")
97+
try:
98+
spec = importer.find_spec("ensure_zipapps")
99+
if spec and spec.loader:
100+
module = spec.loader.load_module("ensure_zipapps")
101+
else:
102+
raise ImportError("Module not found")
103+
except AttributeError:
104+
module = importer.load_module("ensure_zipapps")
105+
del module
106+
sys.modules.pop("ensure_zipapps", None)
107+
```
86108

87109

88110
# Command line usage

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
- `--uv=uv` or `--uv=path/to/uv`
1515
- Added `--freeze-deps` as an alias for `--freeze-reqs`
1616
- fix deprecated warning since 3.10 `zipimporter.load_module`
17+
- add `uvx` use case to test_utils.py
18+
- `uvx zipapps six`
19+
- `python -m zipapps six --uv=uv`
1720
- 2024.08.07
1821
- [**Compatible WARNING**]: update `sys_paths` insert index from `-1` to `0`
1922
- disable `--download-python`, use `python -m zipapps.download_python` instead

test_utils.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import shutil
55
import subprocess
66
import sys
7-
import zipimport
87
from getpass import getuser
98
from pathlib import Path
109
from tempfile import gettempdir
@@ -751,6 +750,7 @@ def test_pip_install_target():
751750

752751
def test_uv_path():
753752
# `pip install uv` before testing
753+
_clean_paths(root=False)
754754
output = subprocess.check_output(
755755
[sys.executable, "-m", "zipapps", "six", "--uv", "uv"], stderr=subprocess.STDOUT
756756
)
@@ -763,6 +763,34 @@ def test_uv_path():
763763
assert b"Collecting six" in output, output.decode("utf-8", "replace")
764764

765765

766+
def test_uvx_zipapps():
767+
# `pip install uv` before testing
768+
_clean_paths(root=False)
769+
if shutil.which("uvx") is None:
770+
raise RuntimeError("uvx not found, please install uvx first")
771+
subprocess.Popen(["uvx", "--with", "..", "zipapps", "-o", "app.pyz", "six"]).wait()
772+
proc = subprocess.Popen(
773+
[sys.executable, "app.pyz", "-c", "import six;print(six.__file__)"],
774+
stderr=subprocess.STDOUT,
775+
stdout=subprocess.PIPE,
776+
)
777+
output = proc.communicate()[0]
778+
assert b"app.pyz" in output, output.decode("utf-8", "replace")
779+
_clean_paths(root=False)
780+
if shutil.which("uvx") is None:
781+
raise RuntimeError("uvx not found, please install uvx first")
782+
subprocess.Popen(
783+
["uvx", "--with", "..", "zipapps", "-o", "app.pyz", "--uv", "uv", "six"]
784+
).wait()
785+
proc = subprocess.Popen(
786+
[sys.executable, "app.pyz", "-c", "import six;print(six.__file__)"],
787+
stderr=subprocess.STDOUT,
788+
stdout=subprocess.PIPE,
789+
)
790+
output = proc.communicate()[0]
791+
assert b"app.pyz" in output, output.decode("utf-8", "replace")
792+
793+
766794
def main():
767795
"""
768796
test all cases

0 commit comments

Comments
 (0)