Python bindings for embedding V8 and running JavaScript, built on
denoland/rusty_v8.
v8-python lets Python code create V8 isolates and contexts, evaluate
JavaScript, pass values between Python and JavaScript, expose Python functions
and classes to JavaScript, and install host APIs such as timers, console,
module loading, and WebAssembly. It is implemented in Rust using
denoland/rusty_v8.
pip install v8-pythonFor local development:
uv run maturin developimportv8isolate=v8.Isolate()
builder=isolate.create_context_builder()
context=builder.build()
result=context.eval("'Hello' + ' from V8'")
print(result)importv8isolate=v8.Isolate()
builder=isolate.create_context_builder()
@builder.host_function(name="add")defadd(left: int, right: int) ->int:
returnleft+rightcontext=builder.build()
print(context.eval("add(20, 22)"))Host APIs are installed through a profile. This keeps the context builder small and makes reusable runtime setups easy to share.
importv8profile=v8.BaseProfile().install([v8.api.Timer()])
isolate=v8.Isolate()
builder=isolate.create_context_builder()
builder.use_profile(profile)
context=builder.build()
context.eval(
""" globalThis.events = []; setTimeout(() => events.push("ready"), 0); """
)
context.run_until_idle(max_tasks=10)
print(context.eval("events.join(', ')"))JavaScript promises can be awaited from Python.
importasyncioimportv8isolate=v8.Isolate()
builder=isolate.create_context_builder()
context=builder.build()
asyncdefmain():
returnawaitcontext.eval("Promise.resolve('done')")
print(asyncio.run(main()))More focused examples are available in the examples/ directory.
uv run --group doc zensical serveMIT License.