Package: wasmer
Wasmer PythonA complete and mature WebAssembly runtime for Python based onWasmer.Features:Easy to use: ThewasmerAPI mimics the standard WebAssembly API,Fast:wasmerexecutes the WebAssembly modules as fast as
possible, close tonative speed,Safe: All calls to WebAssembly will be fast, but more
importantly, completely safe and sandboxed,Modular:wasmercan compile the WebAssembly modules with
different engines or compiler.Documentation:browse the detailed API
documentationfull of
examples.Examplesas tutorials:browse theexamples/directory,
it's the best place for a complete introduction!Quick IntroductionThewasmerpackage brings the required API to execute WebAssembly
modules. In a nutshell,wasmercompiles the WebAssembly module into
compiled code, and then executes it.wasmeris 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, anengineis responsible to drive thecompilationand
theexecutionof a WebAssembly module. By extension, aheadlessengine can only execute a WebAssembly module, i.e. a module that has
previously been compiled, or compiled, serialized and deserialized. By
default, thewasmerpackage comes with 2 headless engines:wasmer.engine.JIT, the compiled machine code lives in memory,wasmer.engine.Native, the compiled machine code lives in a shared
object file (.so,.dylib, or.dll), and is natively executed.Becausewasmerdoes 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 packageDescriptionPyPiwasmer_compiler_singlepassSuper fast compilation times, slower execution times. Not prone to JIT-bombs.Ideal for blockchainswasmer_compiler_craneliftFast compilation times, fast execution times.Ideal for developmentwasmer_compiler_llvmSlow compilation times, very fast execution times (close to native, sometimes faster).Ideal for ProductionWe generally recommendwasmer_compiler_craneliftfor development
purposes andwasmer_compiler_llvmin production.Learn more by readingthe documentation of thewasmer.enginesubmodule.InstallTo install thewasmerPython package, and let's say thewasmer_compiler_craneliftcompiler, just run those commands in your shell:$pipinstallwasmer==1.1.0
$pipinstallwasmer_compiler_cranelift==1.1.0And you're ready to get fun!ExampleWe highly recommend to read theexamples/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 inexamples/appendices/simple.rs, written in Rust:#[no_mangle]pubexternfnsum(x:i32,y:i32)->i32{x+y}After compilation to WebAssembly, theexamples/appendices/simple.wasmbinary file is generated. (Download
it).Then, we can execute it in Python:fromwasmerimportengine,Store,Module,Instancefromwasmer_compiler_craneliftimportCompiler# 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:$pythonexamples/appendices/simple.pyDevelopmentThe Python extension is written inRust, withpyo3andmaturin.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,
checkpyenv). For Rust though, we
advise to userustup, then:$rustupinstallstableTo set up your environment, you'll needjust, and then, install
the prelude of this project:$cargoinstalljust
$just--list# to learn about all the available recipes$justpreludeIt will installpyo3andmaturinfor Python and for Rust. It will
also installvirtualenv.Then, simply run:$source.env/bin/activate
$justbuildapi
$justbuildcompiler-cranelift
$pythonexamples/appendices/simple.pySupported platformsWe try to provide wheels for as many platforms and architectures as
possible. For the moment, here are the supported platforms and
architectures:PlatformArchitectureTriplePackagesLinuxamd64x86_64-unknown-linux-gnuwasmer✅wasmer_compiler_singlepass✅wasmer_compiler_cranelift✅wasmer_compiler_llvm✅aarch64aarch64-unknown-linux-gnuwasmer✅wasmer_compiler_singlepass❌1wasmer_compiler_cranelift✅wasmer_compiler_llvm✅Darwinamd64x86_64-apple-darwinwasmer✅wasmer_compiler_singlepass✅wasmer_compiler_cranelift✅wasmer_compiler_llvm✅Windowsamd64x86_64-pc-windows-msvcwasmer✅wasmer_compiler_singlepass✅wasmer_compiler_cranelift✅wasmer_compiler_llvm❌2Notes:1wasmer_compiler_singlepassdoes not supportaarch64for the moment2wasmer_compiler_llvmis not packaging properly on
  Windows for the momentWheels are all built for the following Python versions:Python 3.7,Python 3.8.Python 3.9.Python 3.10,Learn about the “fallback”py3-none-anywheelpy3-none-any.whlA specialwasmer-$(version)-py3-none-anywheel is built as a
fallback. Thewasmerlibray will be installable, but it will raise
anImportErrorexception saying that “Wasmer is not available on
this system”.This wheel will be installed if none matches before (learn more by
reading thePEP 425, Compatibility Tags for Built
Distributions).TestingBuild all the packages and run the tests:$justbuild-all
$justtestWhat is WebAssembly?Quotingthe 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 ofcommon hardware
capabilitiesavailable on a wide range of platforms.About safety:WebAssembly describes a memory-safe, sandboxedexecution
environment[…].LicenseThe entire project is under the MIT License. Please readtheLICENSEfile.