Skip to main content

Python extension to run WebAssembly binaries

Project description

Wasmer logo Wasmer Python PyPI version Wasmer Python Documentation Wasmer PyPI downloads Wasmer Slack Channel

A complete and mature WebAssembly runtime for Python based on Wasmer.

Features:

  • Easy to use: The wasmer API mimics the standard WebAssembly API,
  • Fast: wasmer executes the WebAssembly modules as fast as possible, close to native speed,
  • Safe: All calls to WebAssembly will be fast, but more importantly, completely safe and sandboxed,
  • Modular: wasmer can compile the WebAssembly modules with different engines or compiler.

Documentation: browse the detailed API documentation full of examples.

Examples as tutorials: browse the examples/ directory, it's the best place for a complete introduction!

Quick Introduction

The wasmer package brings the required API to execute WebAssembly modules. In a nutshell, wasmer compiles the WebAssembly module into compiled code, and then executes it. wasmer is designed to work in various environments and platforms: From nano single-board computers to large and powerful servers, including more exotic ones. To address those requirements, Wasmer provides 2 engines and 3 compilers.

Succinctly, an engine is responsible to drive the compilation and the execution of a WebAssembly module. By extension, a headless engine can only execute a WebAssembly module, i.e. a module that has previously been compiled, or compiled, serialized and deserialized. By default, the wasmer package comes with 2 headless engines:

  1. wasmer.engine.JIT, the compiled machine code lives in memory,
  2. wasmer.engine.Native, the compiled machine code lives in a shared object file (.so, .dylib, or .dll), and is natively executed.

Because wasmer does not embed compilers in its package, engines are headless, i.e. they can't compile WebAssembly module; they can only execute them. Compilers live in their own standalone packages. Let's briefly introduce them:

Compiler package Description PyPi
wasmer_compiler_singlepass Super fast compilation times, slower execution times. Not prone to JIT-bombs. Ideal for blockchains On PyPi
wasmer_compiler_cranelift Fast compilation times, fast execution times. Ideal for development On PyPi
wasmer_compiler_llvm Slow compilation times, very fast execution times (close to native, sometimes faster). Ideal for Production On PyPi

We generally recommend wasmer_compiler_cranelift for development purposes and wasmer_compiler_llvm in production.

Learn more by reading the documentation of the wasmer.engine submodule.

Install

To install the wasmer Python package, and let's say the wasmer_compiler_cranelift compiler, just run those commands in your shell:

$ pip install wasmer==1.0.0a3
$ pip install wasmer_compiler_cranelift==1.0.0a3

And you're ready to get fun!

Example

We highly recommend to the read the examples/ directory, which contains a sequence of examples/tutorials. It's the best place to learn by reading examples.

But for the most eager of you, and we know you're numerous you mischievous, there is a quick toy program in examples/appendices/simple.rs, written in Rust:

#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
    x + y
}

After compilation to WebAssembly, the examples/appendices/simple.wasm binary file is generated. (Download it).

Then, we can excecute it in Python:

from wasmer import engine, Store, Module, Instance
from wasmer_compiler_cranelift import Compiler

# Let's define the store, that holds the engine, that holds the compiler.
store = Store(engine.JIT(Compiler))

# Let's compile the module to be able to execute it!
module = Module(store, open('simple.wasm', 'rb').read())

# Now the module is compiled, we can instantiate it.
instance = Instance(module)

# Call the exported `sum` function.
result = instance.exports.sum(5, 37)

print(result) # 42!

And then, finally, enjoy by running:

$ python examples/appendices/simple.py

Development

The Python extension is written in Rust, with pyo3 and maturin.

First, you need to install Rust and Python. We will not make you the affront to explain to you how to install Python (if you really need, check pyenv). For Rust though, we advise to use rustup, then:

$ rustup install stable

To set up your environment, you'll need just, and then, install the prelude of this project:

$ cargo install just
$ just --list # to learn about all the available recipes
$ just prelude

It will install pyo3 and maturin for Python and for Rust. It will also install virtualenv.

Then, simply run:

$ source .env/bin/activate
$ just build api
$ just build compiler-cranelift
$ just python-run examples/appendices/simple.py

If you need to interact with Python, or run a specific file, use the following commands:

$ just python-run
$ just python-run file/to/run.py

Supported platforms

We try to provide wheels for as many platforms and architectures as possible. For the moment, here are the supported platforms and architectures:

Platform Architecture Triple
Linux amd64 x86_64-unknown-linux-gnu
Linux aarch64 aarch64-unknown-linux-gnu
Darwin amd64 x86_64-apple-darwin
Windows amd64 x86_64-pc-windows-msvc

Wheels are all built for the following Python versions:

  • Python 3.5,
  • Python 3.6,
  • Python 3.7,
  • Python 3.8.
  • Python 3.9.
Learn about the “fallback” py3-none-any wheel

py3-none-any.whl

A special wasmer-$(version)-py3-none-any wheel is built as a fallback. The wasmer libray will be installable, but it will raise an ImportError exception saying that “Wasmer is not available on this system”.

This wheel will be installed if none matches before (learn more by reading the PEP 425, Compatibility Tags for Built Distributions).

Testing

Build all the packages and run the tests:

$ just build-all
$ just test

What is WebAssembly?

Quoting the WebAssembly site:

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable target for compilation of high-level languages like C/C++/Rust, enabling deployment on the web for client and server applications.

About speed:

WebAssembly aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.

About safety:

WebAssembly describes a memory-safe, sandboxed execution environment […].

License

The entire project is under the MIT License. Please read the LICENSE file.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

wasmer-1.0.0b1-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

wasmer-1.0.0_beta1-cp39-cp39-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.9

wasmer-1.0.0_beta1-cp39-cp39-macosx_10_7_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9 macOS 10.7+ x86-64

wasmer-1.0.0_beta1-cp38-cp38-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.8

wasmer-1.0.0_beta1-cp38-cp38-macosx_10_7_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8 macOS 10.7+ x86-64

wasmer-1.0.0_beta1-cp37-cp37m-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.7m

wasmer-1.0.0_beta1-cp37-cp37m-macosx_10_7_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.7m macOS 10.7+ x86-64

wasmer-1.0.0_beta1-cp36-cp36m-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.6m

wasmer-1.0.0_beta1-cp36-cp36m-macosx_10_7_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.6m macOS 10.7+ x86-64

wasmer-1.0.0_beta1-cp35-cp35m-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 3.5m

wasmer-1.0.0_beta1-cp35-cp35m-macosx_10_7_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.5m macOS 10.7+ x86-64

File details

Details for the file wasmer-1.0.0b1-py3-none-any.whl.

File metadata

  • Download URL: wasmer-1.0.0b1-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 976c1b1083c6878341bb66294c24fded858c68b54994a74c456e01e349a865c5
MD5 9f39c479069a6732e2d60514a9c9def7
BLAKE2b-256 a2413584aff8b27b5c4ac9c5a7230730ee014b08d4caf828fe4580e890ff9c4d

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 66e773be4775be58c2f31dd4f9bc891f2c86fadfa1185030ffe4075be1697385
MD5 76c4f159028c843942a8aebc04841a2b
BLAKE2b-256 ebf0a3dbd15c1f0e5c19a9cb4e6be0f3f6c345ffd0a916e819e11da95cefcb5a

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp39-cp39-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp39-cp39-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.9, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp39-cp39-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 3da6ceba0702ca97439fb7551c8a2255a2b558171c3ccbbfe5db59407d6468ab
MD5 a418f0efa59b9285afb56bc0de649e9b
BLAKE2b-256 dd51e2ae48e7ffd526e3ba44a02b07a730047cacdca7f116864124f9b21c56ba

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b030c66ce32c1d4bcfa80c692cf917dff38a224f1bc49b8998def722303d9740
MD5 0bbf9b92959d08f1e44770bc22b0b57b
BLAKE2b-256 c29c68df306df65347f840767154ef79c87b810a53d6bc2d8541f76a7d0acd1f

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp38-cp38-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp38-cp38-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.8, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp38-cp38-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 d38b371323f701b8808671eb7d361b727dbea4009978009866a4d56694f772ac
MD5 b532a6d8d0e48e00a729feb77f9595de
BLAKE2b-256 57a5f0548ea475e5edcc676ffddcec648a782610b3f36b6bc4d9577a38532202

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 63437b3b5d0d6566a5de9748080197b6a31320ca3b7c1775342dfdc60148361f
MD5 73309d253dad8fbd098e8a797279f1fb
BLAKE2b-256 79fa16b702472d470f6191fc8625233c265dfeb1464ca2e9d1cc58ee3e372d4c

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp37-cp37m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp37-cp37m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.7m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp37-cp37m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5817e8b3cf609dc545fa7929ad3d25299c39a8a3c524f443169ee808986fd468
MD5 d6fde17da5633e4001a64a3fdd5419c5
BLAKE2b-256 3e4f0cdf2d00ee4f506ae1a6b01dff7f3717fff2bbeb7626fb5b44a8e8cb4784

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 020cd132012b6648196ba7c9238f971e5dcfa5b892afd012fefb20d9e36acc8e
MD5 17958183eec8dc03a0bb6b934447e4c7
BLAKE2b-256 27830bfb1ac7b8a2140d77591e7dc262fb4cebaed9261e4368e136edfb1abd46

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp36-cp36m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp36-cp36m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.6m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp36-cp36m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 155eb1c375a9b2e55537ca4d95719fac23f333dd843096c778e8669e2dba2e25
MD5 c864fd4cbd2ecdf13b05fb99593e2fe2
BLAKE2b-256 6fec0564c7e6e01e66835498c8bfe42aef13e1bae4d05ed1acf65b5a5b079bdd

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp35-cp35m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 1.5 MB
  • Tags: CPython 3.5m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ddc24697c36e37d691394f354c6f3e7f446c1f9b8a5174d785200095afe6211a
MD5 e39a6f253129a9092afb676d2e3a4f17
BLAKE2b-256 926f5a5701d2dc02a79293f47ffda1e5e39cda49f4f5a34c5f659ff765c9f7b5

See more details on using hashes here.

File details

Details for the file wasmer-1.0.0_beta1-cp35-cp35m-macosx_10_7_x86_64.whl.

File metadata

  • Download URL: wasmer-1.0.0_beta1-cp35-cp35m-macosx_10_7_x86_64.whl
  • Upload date:
  • Size: 1.4 MB
  • Tags: CPython 3.5m, macOS 10.7+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.9.0

File hashes

Hashes for wasmer-1.0.0_beta1-cp35-cp35m-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 5dcf9da895d4cde8d099e820551b0e1a6036a2c15e4a0e0a2715fc57504e8196
MD5 f18dfe3482665b14663422967d86c47e
BLAKE2b-256 8daccc4a887a5a49dcd625494974d4e31e23cd6dd324a1938c50776e6b8500cf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page