pythonwebassemblypyodide

Unable to import a python package in Pyodide


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'


Solution

  • 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,