PyExecJS
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.
Common errors
-
EnvironmentError: Your Javascript runtime 'JScript' not supported due to security concerns.
cause PyExecJS attempted to use the JScript runtime on Windows, which has been explicitly disabled due to security vulnerabilities or is no longer considered viable.fixInstall 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. -
execjs.RuntimeUnavailableError: No runtime available
cause PyExecJS could not find any compatible JavaScript runtime (e.g., Node.js, PyV8, JavaScriptCore, SpiderMonkey) installed on the system, or they are not correctly configured/in the system's PATH.fixInstall 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. -
ProgramError: SyntaxError: Unexpected token var
cause This error often occurs when JavaScript code is passed incorrectly to `execjs.eval()` or `execjs.compile()`, or when there's an actual syntax error in the JavaScript string. A common mistake is trying to embed Python variables directly into the JavaScript string without proper concatenation or formatting.fixEnsure 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')`.
Warnings
- breaking PyExecJS is End-of-Life (EOL) and no longer actively maintained since 2018. Bugs will not be fixed, and its original motivation (bridging JavaScript runtimes on Windows XP) is obsolete. Users are advised to use alternative libraries or directly interact with JavaScript runtimes via `subprocess`.
- deprecated The JScript runtime (Microsoft Windows Script Host) is not supported due to security concerns. Attempting to use it will result in an `EnvironmentError`.
- gotcha PyExecJS communicates with JavaScript runtimes primarily via text, which can lead to performance overhead, especially for frequent or complex operations.
- gotcha PyExecJS does not fully support runtime-specific features (e.g., Node.js environment variables or module loading mechanisms). It's designed as a generic adapter, which limits its utility for advanced Node.js interactions.
Install
-
pip install PyExecJS
Imports
- execjs
import execjs
Quickstart
import execjs
import os
# Basic evaluation
result_eval = execjs.eval("'red yellow blue'.split(' ')")
print(f"Eval result: {result_eval}")
# Compiling and calling a function
ctx = execjs.compile("""
function add(x, y) {
return x + y;
}
""")
result_call = ctx.call("add", 1, 2)
print(f"Call result: {result_call}")
# Selecting a specific runtime (requires runtime to be installed, e.g., Node.js)
# You might need to set the environment variable or ensure Node.js is in PATH
# os.environ["EXECJS_RUNTIME"] = "Node"
# node_runtime = execjs.get()
# if node_runtime:
# print(f"Selected runtime: {node_runtime.name}")
# print(f"Node.js eval result: {node_runtime.eval('1 + 2')}")
# else:
# print("Node.js runtime not found or configured.")