Loading custom Python code — Version 0.25.0 (2024)

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() toinstall the package. See Loading packages for more information.

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 archiveawait 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 fileawait 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();

What is pyfetch?

Pyodide provides 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, "gztar"); // by default, unpacks to the current dirpyodide.pyimport("your_package");

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 manuallybefore 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());
Loading custom Python code — Version 0.25.0 (2024)
Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5565

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.