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.
pip install py-mini-racerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a `MiniRacer` context and execute basic JavaScript code using `eval()` and `call()`. `eval()` is suitable for primitive return types, while `call()` is used for composite types like objects, which are serialized via JSON.
Migrate to the `mini-racer` library by installing `pip install mini-racer` and updating import statements if necessary. Consult the `mini-racer` documentation for migration details.
Review application architecture for multi-process or multi-threaded JavaScript execution. If multi-threading within V8 is critical, investigate the `mini-racer` successor which might offer different configuration options or alternative solutions.
Ensure all JavaScript code executed within PyMiniRacer is compliant with ECMAScript strict mode. This typically involves fixing common JavaScript 'sloppy mode' practices.
Prefer installing pre-built wheels (`pip install py-mini-racer`). If building from source is necessary, ensure you have sufficient disk space, time, and the correct Python 2.7 environment for the V8 build steps (even if using Python 3 for the final package). Consider using the `mini-racer` package, which has more modern build processes.
Use `ctx.call("functionName", args...)` when expecting JavaScript objects, arrays, or other composite structures as return values. Use `ctx.eval("jsExpression")` for primitive returns.Design your application to handle callbacks from JavaScript to Python using alternative mechanisms (e.g., polling for results, or re-evaluating context in Python). The successor `mini-racer` introduced `wrap_py_function` for this functionality in v0.12.0.
Migrate to the `mini-racer` library by installing `pip install mini-racer` and updating import statements from `from py_mini_racer import MiniRacer` to `from mini_racer import MiniRacer`.
Explicitly manage the `MiniRacer` instance's lifecycle or, more reliably, migrate to the actively maintained `mini-racer` library which has improved memory management and provides a context manager for proper teardown.
Ensure JavaScript execution is isolated to single threads or separate processes. For robust solutions and improved stability, migrate to the `mini-racer` library, which features significant enhancements in threading and memory management to prevent such crashes.
For `py-mini-racer`, direct Python-to-JavaScript callbacks are not supported. If this functionality is required, migrate to the `mini-racer` library (version 0.12.0 and above), which introduced the capability to expose asynchronous Python functions to the JavaScript context.