I would like to refactor my code in several python files and therefore use functions written in a .py file from another .py file. The two python files are in the same directory, so I try to import the file with the “import …” syntax but a ModuleNotFoundError is raised.
Can anyone tell me if what I want to do is possible and how?
Yes indeed! If you add the <py-config> element to the page, you can use one of its parameters to load files from the network into the Emscripten local file system that Pyodide creates “within the browser window”, so to speak. This local file system is where Python looks for files when using import.
Let’s say we want to load a file from the url some/url/to/mymodule.py to a file located at ./mymodule.py in the local filesystem. That is to say, in the same folder that Python scripts are executed from.
In the current release (2022.09.1), the syntax is:
The upcoming release (2022.12.1 probably) introduces a powerful new feature called “Fetch Configurations”; there are a few ways to get the file where it’s wanted with a fetch configuration, but for this URL and destination, one solution is: