PyExecJS is a Python library (version 1.5.1) designed to run JavaScript code from Python by automatically picking the best available JavaScript runtime. It was developed as a port of Ruby's ExecJS, aiming to simplify JavaScript execution, particularly in environments like Windows XP where Node.js was not readily available. However, the library is officially End-of-Life (EOL) since 2018 and is no longer maintained, with its original purpose largely superseded by modern JavaScript runtime availability.
pip install PyExecJSVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the core functionalities of PyExecJS: `execjs.eval()` for simple JavaScript snippets and `execjs.compile()` followed by `ctx.call()` for more complex code with functions. It also shows how to (optionally) attempt to select a specific runtime, though proper runtime installation and environment configuration are external requirements.
Migrate to direct `subprocess` calls for Node.js, `python-uglifyjs`, `js2py`, or a specialized library for your specific use case.
Ensure Node.js, PyV8, or another supported JavaScript runtime is installed and available in your system's PATH, or explicitly configured for PyExecJS.
For performance-critical applications, consider using a direct integration with a JavaScript engine (e.g., PyV8 if actively maintained, or a more performant JavaScript-Python bridge) or minimize the number of calls by processing larger batches of data within the JavaScript context.
If specific runtime features are required, it's recommended to bypass PyExecJS and invoke the JavaScript runtime directly using Python's `subprocess` module, allowing full control over the execution environment and arguments.
Install a supported JavaScript runtime like Node.js and ensure it's accessible in your system's PATH. PyExecJS will automatically try to detect and use it. You can verify detected runtimes programmatically.
Install a JavaScript runtime like Node.js (`npm install -g node` or download from nodejs.org) and ensure it's added to your system's PATH. On macOS, JavaScriptCore is usually available. On systems where PyV8 is an option, ensure it's correctly installed and configured. Restart your environment after installation.
Ensure the JavaScript code passed is a valid, self-contained JavaScript string. If you need to include dynamic Python values, use f-strings or `.format()` to inject them into the JavaScript string before passing it to PyExecJS. For instance, `execjs.eval(f'"Hello, {python_var}!".length')`.