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:

  1. create a Python package

  2. load your Python package into the Pyodide (Emscripten) virtual file system

  3. import the package with let mypkg = pyodide.pyimport("mypkgname")

  4. call into your package with mypkg.some_api(some_args).

Using wheels#

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 micropip.install to 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.

From Python#

// 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");
// 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");

What is pyfetch?

Pyodide provides pyodide.http.pyfetch, which is a convenient wrapper of JavaScript fetch. See How can I load external files in Pyodide? for more information.

From JavaScript#

let response = await fetch("https://.../your_package.tar.gz"); // .zip, .whl, ...
let buffer = await response.arraybuffer();
await pyodide.unpackArchive(buffer); // by default, unpacks to the current dir

Warning on unpacking a wheel package

Since a wheel package is actually a zip archive, you can use pyodide.unpackArchive() to unpack a wheel package, instead of using micropip.install.

However, Micropip does dependency resolution when installing packages, while 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());