Skip to main content

componentize-py

A Bytecode Alliance project

This is a tool to convert a Python application to a WebAssembly component. It takes the following as input:

  • a WIT file or directory
  • the name of a WIT world defined in the above file or directory
  • the name of a Python module which targets said world
  • a list of directories in which to find the Python module and its dependencies

The output is a component which may be run using e.g. wasmtime.

Getting Started

First, install Python 3.10 or later and pip if you don't already have them. Then, install componentize-py:

pip install componentize-py

Next, create or download the WIT world you'd like to target, e.g.:

cat >hello.wit <<EOF
package example:hello;
world hello {
  export hello: func() -> string;
}
EOF

If you're using an IDE or just want to examine the bindings produced for the WIT world, you can generate them using the bindings subcommand:

componentize-py -d hello.wit -w hello bindings hello_guest

Then, use the hello module produced by the command above to write your app:

cat >app.py <<EOF
import wit_world
class WitWorld(wit_world.WitWorld):
    def hello(self) -> str:
        return "Hello, World!"
EOF

And finally generate the component:

componentize-py -d hello.wit -w hello componentize --stub-wasi app -o app.wasm

To test it, you can install wasmtime-py and use it to generate host-side bindings for the component:

[!NOTE] Due to compatibility issues with wasmtime-py versions beyond 38.x, this example requires version 38.0.0 or earlier: pip install "wasmtime==38.0.0"

pip install wasmtime
python3 -m wasmtime.bindgen app.wasm --out-dir hello_host

Now we can write a simple host app using those bindings:

cat >host.py <<EOF
from hello_host import Root
from wasmtime import Config, Engine, Store

config = Config()
config.cache = True
engine = Engine(config)
store = Store(engine)
hello = Root(store)
print(f"component says: {hello.hello(store)}")
EOF

And finally run it:

 $ python3 host.py
component says: Hello, World!

See the examples directories for more examples, including various ways to run the components you've created.

Known Limitations

Currently, the application can only import dependencies during build time, which means any imports used at runtime must be resolved at the top level of the application module. For example, if x is a module with a submodule named y the following may not work:

import x

class Hello(hello.Hello):
    def hello(self) -> str:
        return x.y.foo()

That's because importing x does not necessarily resolve y. This can be addressed by modifying the code to import y at the top level of the file:

from x import y

class Hello(hello.Hello):
    def hello(self) -> str:
        return y.foo()

This limitation is being tracked as issue #23.

See the issue tracker for other known issues.

Contributing

See CONTRIBUTING.md for details on how to contribute to the project and build it from source.

Metadata

Release files for factored-componentize-py 0.20.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for factored-componentize-py 0.20.7
File Interpreter ABI Platform Actions
factored_componentize_py-0.20.7-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details for factored_componentize_py-0.20.7-cp39-abi3-win_amd64.whl
factored_componentize_py-0.20.7-cp39-abi3-manylinux_2_34_x86_64.whl CPython 3.9 abi3 Linux glibc 2.34+ x86-64 Details for factored_componentize_py-0.20.7-cp39-abi3-manylinux_2_34_x86_64.whl
factored_componentize_py-0.20.7-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details for factored_componentize_py-0.20.7-cp39-abi3-macosx_11_0_arm64.whl

Total release size: 68.9 MB

Release files / factored_componentize_py-0.20.7-cp39-abi3-win_amd64.whl

Download URL factored_componentize_py-0.20.7-cp39-abi3-win_amd64.whl
Size 22.2 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
c952e8459ad15a6e570cb313afdd0a87933e703898753448a0f7f680fd1b1ba4
BLAKE2b-256 checksum
How to use checksums
02195c5247d2dbcb1fb62cc85904d0e26b502d742de6abf63eab260b7a1fc5c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.13.3

Release files / factored_componentize_py-0.20.7-cp39-abi3-manylinux_2_34_x86_64.whl

Download URL factored_componentize_py-0.20.7-cp39-abi3-manylinux_2_34_x86_64.whl
Size 24.0 MB
Tags CPython 3.9 Linux glibc 2.34+ x86-64 abi3
SHA-256 checksum
How to use checksums
cb2eccd49182a35d258d93e6dde20bf92f03acff646493ac406e85abb59caf84
BLAKE2b-256 checksum
How to use checksums
5a57aa505be1ce222253bc4240a6ee2a3f897d9e3dd2e35b7852db5aa3fcee16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.13.1

Release files / factored_componentize_py-0.20.7-cp39-abi3-macosx_11_0_arm64.whl

Download URL factored_componentize_py-0.20.7-cp39-abi3-macosx_11_0_arm64.whl
Size 22.7 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
df3244974750a37ddba92a033ed06512f20b09a528e5f6caca0babf91ff7f02f
BLAKE2b-256 checksum
How to use checksums
e34becccc61797c9f64f6445b3d670bca134c1cd98ea0b4a321a99da0efd206f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.13.1

Security for factored-componentize-py 0.20.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page