Loading custom Python code#
Pyodide provides a simple API
pyodide.runPython() to run Python code.
However, when your Python code grow bigger, putting hundreds of lines inside
runPython is not scalable.
For larger projects, the best way to run Python code with Pyodide is:
create a Python package
load your Python package into the Pyodide (Emscripten) virtual file system
import the package with
let mypkg = pyodide.pyimport("mypkgname")
call into your package with
The best way of serving custom Python code is making it a package in the wheel (.whl) format.
If the package is built as a
wheel file, you can use
install the package. See Loading packages for more information.
Packages with C extensions
If your Python code contains C extensions, it needs to be built in a specialized way (See Creating a Pyodide package).
Loading then importing Python code#
It is also possible to download and import Python code from an external source. We recommend that you serve all files in an archive, instead of individually downloading each Python script.
// Downloading an archive 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();
// Downloading a single file await pyodide.runPythonAsync(` from pyodide.http import pyfetch response = await pyfetch("https://.../script.py") with open("script.py", "wb") as f: f.write(await response.bytes()) `) pkg = pyodide.pyimport("script"); pkg.do_something();
let response = await fetch("https://.../your_package.tar.gz"); // .zip, .whl, ... let buffer = await response.arrayBuffer(); await pyodide.unpackArchive(buffer, "gztar"); // by default, unpacks to the current dir pyodide.pyimport("your_package");
Warning on unpacking a wheel package
micropip does dependency resolution when installing packages,
pyodide.unpackArchive() simply unpacks the archive.
So you must be aware of that each dependencies of a package need to be installed manually
before unpacking a wheel.
Future plans: we are planning to support a method for a static dependency resolution (See: pyodide#2045).
Running external code directly#
If you want to run a single Python script from an external source in a simplest way, you can:
pyodide.runPython(await (await fetch("https://some_url/.../code.py")).text());