PyMiniRacer (Original - Deprecated)
PyMiniRacer is a Python library that provides a minimal and modern embedded V8 JavaScript engine for Python. It allows Python applications to execute JavaScript code, supporting the latest ECMAScript features and Web Assembly. Developed by Sqreen, this original project is at version 0.6.0 and has been superseded by the `mini-racer` library under a new maintainer.
Warnings
- breaking The `py-mini-racer` library (sqreen/PyMiniRacer) is deprecated and no longer actively maintained. The recommended successor project is `mini-racer` (bpcreech/PyMiniRacer), which provides updated V8 versions and continued development. Users are strongly encouraged to migrate.
- breaking As of v0.6.0, V8 was switched to single-threaded mode by default to prevent crashes after process forks. If your application relied on multi-threaded V8 operations within the same process or expected specific behavior after forking, this change might affect stability or performance.
- breaking Version 0.6.0 switched V8 to strict mode by default. JavaScript code that previously ran without issues in non-strict mode (e.g., using undeclared variables, duplicate parameter names) might now throw errors.
- gotcha Building PyMiniRacer from source is a resource-intensive process, requiring several GB of disk space and approximately 60 minutes. Furthermore, the V8 build system historically required a Python 2.7 executable.
- gotcha The `eval()` method in `py-mini-racer` only supports returning primitive data types (numbers, strings, buffers). To return composite types like JavaScript objects or arrays, you must use the `call()` method.
- gotcha This version of PyMiniRacer does not support attaching Python objects or functions to the JavaScript context, meaning you cannot directly call Python code from JavaScript.
Install
-
pip install py-mini-racer
Imports
- MiniRacer
from py_mini_racer import MiniRacer
Quickstart
from py_mini_racer import MiniRacer
# Create a new MiniRacer context
ctx = MiniRacer()
# Evaluate JavaScript code
result = ctx.eval("1 + 1")
print(f"1 + 1 = {result}")
# Evaluate more complex JavaScript with variables
ctx.eval("var x = {company: 'Acme'};")
company_name = ctx.eval("x.company")
print(f"Company name: {company_name}")
# Use .call() for composite types, .eval() for primitives
def_func = ctx.eval("var fun = () => ({ foo: 1 });")
composite_result = ctx.call("fun")
print(f"Composite result: {composite_result}")