micropip icon indicating copy to clipboard operation
micropip copied to clipboard

Support installing python wheel from local file system

Open YalinLi0312 opened this issue 1 year ago • 5 comments

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?

YalinLi0312 avatar Oct 18 '24 23:10 YalinLi0312

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")

ryanking13 avatar Oct 19 '24 12:10 ryanking13

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.

YalinLi0312 avatar Oct 25 '24 17:10 YalinLi0312

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 avatar Oct 25 '24 20:10 hoodmane

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 avatar Oct 25 '24 20:10 hoodmane

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).

hoodmane avatar Oct 25 '24 20:10 hoodmane