Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyodide pack: Add support for packages installed with micropip #31

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add support for stdlib bundling in `pyodide pack`
[#27](https://github.com/pyodide/pyodide-pack/pull/27)

- Add support for packages installed via micropip in `pyodide pack`
[#31](https://github.com/pyodide/pyodide-pack/pull/31)


### Changed

Expand Down
6 changes: 6 additions & 0 deletions examples/micropip_deps/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Examples of a package that is installed by micropip from PyPI

import snowballstemmer

stemmer = snowballstemmer.stemmer("english")
print(stemmer.stemWords("An example of stemming".split()))
1 change: 1 addition & 0 deletions examples/micropip_deps/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
snowballstemmer
9 changes: 9 additions & 0 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def test_all(example_dir, tmp_path):

stdout_str = stdout.getvalue()
assert "Bundle validation successful" in stdout_str
using_micropip = (
"Failed to load packages with loadPackage, re-trying with micropip."
in stdout_str
)

if example_dir.name == ["micropip_deps"]:
assert using_micropip
else:
assert not using_micropip
assert (tmp_path / "python_stdlib_stripped.zip").exists()
# Better than 20% size reduction on the stdlib
assert (tmp_path / "python_stdlib_stripped.zip").stat().st_size < (
Expand Down
8 changes: 8 additions & 0 deletions pyodide_pack/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import sys
import tempfile
import urllib.request
from contextlib import contextmanager
from pathlib import Path

Expand Down Expand Up @@ -117,6 +118,13 @@ def _get_packages_from_lockfile(
for key, val in loaded_packages.items():
if val == "default channel":
file_name = pyodide_lock.packages[key].file_name
elif val == "pypi":
url = pyodide_lock.packages[key].file_name
file_name = os.path.basename(url)
# Cache wheels in node_modules/pyodide
# Will raise an exception if the URL is not valid
with urllib.request.urlopen(url) as response:
(package_dir / file_name).write_bytes(response.read())
else:
# Otherwise loaded from custom URL
# TODO: this branch needs testing
Expand Down
5 changes: 4 additions & 1 deletion pyodide_pack/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ def main(

package_dir = ROOT_DIR / "node_modules" / "pyodide"

pyodide_lock = PyodideLockSpec.from_json(package_dir / "pyodide-lock.json")
if "pyodide_lock" in db:
pyodide_lock = PyodideLockSpec(**json.loads(db["pyodide_lock"]))
else:
pyodide_lock = PyodideLockSpec.from_json(package_dir / "pyodide-lock.json")

packages = _get_packages_from_lockfile(
pyodide_lock, db["loaded_packages"], package_dir
Expand Down
12 changes: 11 additions & 1 deletion pyodide_pack/js/discovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ async function main() {
return open_orig(path, flags, mode, fd_start, fd_end);
};

await pyodide.loadPackage({{packages}});
try {
await pyodide.loadPackage({{packages}});
} catch (e) {
console.log("Failed to load packages with loadPackage, re-trying with micropip.");
await pyodide.loadPackage("micropip");
let micropip = pyodide.pyimport("micropip");
await micropip.install({{packages}});
}

// Monkeypatching findObject calls used in dlopen
let findObjectCalls = [];
Expand All @@ -42,6 +49,9 @@ async function main() {
obj.loaded_packages = pyodide.loadedPackages;
obj.find_object_calls = findObjectCalls;
obj.sys_modules = sysModules;
if ("micropip" in pyodide.loadedPackages) {
obj.pyodide_lock = pyodide.pyimport("micropip").freeze();
}
// For some reason there is a double / in the path
obj.stdlib_prefix = pyodide.pyimport('sysconfig').get_path('stdlib').replace('//', '/');
let jsonString = JSON.stringify(obj);
Expand Down