Is local module import broken?

I was having trouble importing local modules, getting a “ModuleNotFoundError.” Sure that I was setting up py-config correctly, I copied and pasted the example scripts from the documentation (see below) and got the same error. Is this a known issue or is it a matter of the documentation lagging behind the latest version a bit?

# data.py
import numpy as np
def make_x_and_y(n):
    x = np.random.randn(n)
    y = np.random.randn(n)
    return x, y
<html>
    <head>
      <link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
      <script defer src="https://pyscript.net/latest/pyscript.js"></script>
    </head>

  <body>
    <h1>Let's plot random numbers</h1>
    <div id="plot"></div>
    <py-config type="toml">
        packages = ["numpy", "matplotlib"]

        [[fetch]]
        files = ["./data.py"]
    </py-config>
    <py-script output="plot">
      import matplotlib.pyplot as plt
      from data import make_x_and_y
      x, y = make_x_and_y(n=1000)
      fig, ax = plt.subplots()
      ax.scatter(x, y)
      fig
    </py-script>
  </body>
</html>

When loading the page, this error is printed

“PythonError: Traceback (most recent call last): File “/lib/python3.10/asyncio/futures.py”, line 201, in result raise self._exception File “/lib/python3.10/asyncio/tasks.py”, line 232, in __step result = coro.send(None) File “/lib/python3.10/site-packages/_pyodide/_base.py”, line 506, in eval_code_async await CodeRunner( File “/lib/python3.10/site-packages/_pyodide/_base.py”, line 357, in run_async coroutine = eval(self.code, globals, locals) File “”, line 2, in ModuleNotFoundError: No module named ‘data’”

Hi Mark,

Unfortunately it’s our documentation that is broken, not local module import. Right now, we update docs.pyscript.net whenever new docs get merged into the GtiHub repo, but those changes aren’t available in a release until…well, until the next release.

At this exact moment (in 2022.09.1), the syntax to load files from a URL into the Emscirpten file system is:

<py-config>
  paths = ['data.py']
</py-config>

Which will load the resource at the relative URL “data.py” to the local path “./data.py”

The doc issue is unfortunate - especially with the number of great-but-breaking changes recently. It’s being tracked as #937 on GitHub, and we hope to have a resolution soon.

I would expect that’s just the nature of the beast when you’re developing a framework in ‘real time’. I appreciate the work you and the other maintainers have put into this and thanks for the info on imports.