Skip to main content

Using the Python Host SDK

Check your installation

Please be sure you've installed Extism before continuing with this guide.

1. Install the Python library

Install via Pip:

pip3 install extism

Install via Poetry:

poetry add extism

2. Import the library and use the APIs

Count Vowels Plugin

code.wasm in this example is our example plugin that counts vowels. If you want to run this, download it first and set the path:

curl > code.wasm
import sys
import json
import hashlib
import pathlib

from extism import Context

# Compare against Python implementation.
def count_vowels(data):
return sum(letter in b"AaEeIiOoUu" for letter in data)

def main(args):
if len(args) > 1:
data = args[1].encode()
data = b"some data from python!"

wasm_file_path = pathlib.Path(__file__).parent.parent / "wasm" / "code.wasm"
wasm = wasm_file_path.read_bytes()
hash = hashlib.sha256(wasm).hexdigest()
config = {"wasm": [{"data": wasm, "hash": hash}], "memory": {"max": 5}}

# a Context provides a scope for plugins to be managed within. creating multiple contexts
# is expected and groups plugins based on source/tenant/lifetime etc.
with Context() as context:
# NOTE: if you encounter an error such as:
# "Unable to load plugin: unknown import: wasi_snapshot_preview1::fd_write has not been defined"
# pass `wasi=True` in the following function to provide WASI imports to your plugin.
plugin = context.plugin(config)
# Call `count_vowels`
wasm_vowel_count = json.loads("count_vowels", data))

print("Number of vowels:", wasm_vowel_count["count"])

assert wasm_vowel_count["count"] == count_vowels(data)

if __name__ == "__main__":

Host Functions

It is also possible to create functions to expose additional functionality from the host by using Host Functions.

Count Vowels Plugin

To run this example, use the version of the count vowels plugin with the example host function:

curl > code.wasm
from extism import host_fn, Function, ValType

def hello_world(plugin, input_, output, a_string, another_string):
print("Hello from Python!")

# Print input argument
mem = plugin.memory_at_offset(input_[0])

# Print user data

# Set output to input
output[0] = input_[0]

Then add it to the plugin when it's created:

functions = [
"Hello again!",
"Hello once more!",

plugin = context.plugin(config, wasi=True, functions=functions)

Need help?

If you've encountered a bug or think something is missing, please open an issue on the Extism GitHub repository.

There is an active community on Discord where the project maintainers and users can help you. Come hang out!