I am trying to import a custom package in Pyodide
but it is not working.
The directory structure is
index.html
package/
__init__.py
And my index.html looks like this
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/pyodide/v0.19.1/full/pyodide.js"></script>
</head>
<body>
Pyodide test page <br />
Open your browser console to see Pyodide output
<script type="text/javascript">
async function main() {
let pyodide = await loadPyodide({
indexURL: "https://cdn.jsdelivr.net/pyodide/v0.19.1/full/",
});
let mypkg = pyodide.pyimport("package");
mypkg.main();
}
main();
</script>
</body>
</html>
Does anyone know why this might be happening?
I am getting the error
ModuleNotFoundError: No module named 'package'
In the case of Pyodide, Python runs directly in the browser and as such, it cannot see files in your local file system. It can only import modules in the virtual file system provided by Emscripten.
As described in the Pyodide documentation, you would need to copy your files into the virtual file system for it to work. There are two possibilities,
python -m http.server
in this folder.import micropip
await micropip.install('<url-to-the-wheel.whl>')
import your_package
zip -r package.zip package/
await pyodide.runPythonAsync(`
from pyodide.http import pyfetch
response = await pyfetch("https://.../your_package.tar.gz") # .zip, .whl, ...
await response.unpack_archive() # by default, unpacks to the current dir
`)
pkg = pyodide.pyimport("your_package");
pkg.do_something();
If you extract this archive to a different folder than the local folder, you would need to add it to sys.path
for Python to find it.