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

Support installing python wheel from local file system #144

Open
YalinLi0312 opened this issue Oct 18, 2024 · 5 comments
Open

Support installing python wheel from local file system #144

YalinLi0312 opened this issue Oct 18, 2024 · 5 comments

Comments

@YalinLi0312
Copy link

From this doc, I just found the way to import custom python package from url: https://pyodide.org/en/stable/usage/loading-packages.html#installing-wheels-from-arbitrary-urls
While in my use case, I have a python package for internal use only, we don't want to store it in some remote place. So, I'm wondering is it possible to install the python wheel from local file system?

@ryanking13
Copy link
Member

ryanking13 commented Oct 19, 2024

After mounting a local file system into Pyodide, you can use emfs:// prefix to install packages from the local file system.

import micropip

# assuming that the wheel exists in /mnt/pkg/your_package-1.0.0.whl
await micropip.install("emfs:/mnt/pkg/your_package-1.0.0.whl")

@YalinLi0312
Copy link
Author

Thanks @ryanking13 , it works after adding the prefix.

Instead of installing wheel, can I install the local package directly like using pip, e.g. pip install -e ./pkg? In pyodide, I tried this await micropip.install("emfs:/mnt/pkg") but failed.

@hoodmane
Copy link
Member

hoodmane commented Oct 25, 2024

If you make a pyodide virtual environment, you can use pip to install packages and then mount the site-packages folder of the virtual environment as a NodeFS folder and then add it to the path:

pip install pyodide-build
pyodide venv .venv-pyodide
.venv-pyodide/bin/pip install -e path/to/mypkg

to set up the virtual environment. Then load Pyodide, mount .venv-pyodide/ as a node directory and add .venv-pyodide/lib/python3.12/site-packages/ to the path. Something like this should work I think. It is even possible to make the Python environment pick up the virtual environment automatically, but I will have to investigate to remember how.

@hoodmane
Copy link
Member

If you do:

import {loadPyodide} from "pyodide";

const py = await loadPyodide();
py.runPython("import sys; print(sys.executable)");

and then run that file with node, it will print the path to the current file. To make it start in the virtual environment, we need to make sys.executable point instead to .venv-pyodide/bin/python. I'm not sure how it's getting the value in sys.executable though.

@hoodmane
Copy link
Member

hoodmane commented Oct 25, 2024

If what you want to do is run pytest, you could do something like:

.venv-pyodide/bin/pip install pytest
.venv-pyodide/bin/pip install -e ./path/to/src
.venv-pyodide/bin/pytest tests/test_file.py

and it should work quite nicely (unless you need pytest-asyncio which isn't quite set up correctly yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants