PyScript: How to import local libraries?

All the examples I’ve seen show how to include a simple module. How would I import a local library? Every way I try it I get ModuleNotFoundError.

1 Like

Provide more details such as the code and actual error messages. Typically you would specify the modules using <py-env> and then an import statement.

1 Like

I was trying to use pyecore library which is not supported by pyodide.

EDIT: Actually pyecore is available in pyodide…the fist time I tried I got an error and I assumed it was not. But the initial question still remains, suppose I have a custom library just like pyecore.

Project structure:

- index.html
- test.py
- pyecore/
  - __init__.py
  - ecore.py
  - ...

index.html

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <py-env>
        - paths:
          - /test.py
          - /pyecore/ecore.py
    </py-env>
  </head>
  <body>
    <py-script>
      from test import test
      test()
    </py-script>
  </body>
</html>

test.py (but also other libraries) import pyecore simply by:

from pyecore.ecore import EClass

The error I get is:
File "", line 1, in File "/home/pyodide/test.py", line 10, in from pyecore.ecore import EClass ModuleNotFoundError: No module named 'pyecore'

1 Like

After a bit more digging I found a workaround mentioned in this forum. There is also an issue submitted on github about this.

The workaround suggested is to make a prebuilt wheel and include it like a normal module.

<py-env>
- ./pyecore.whl
</py-env>