PyExecJS

1.5.1 · abandoned · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

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.

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.")

view raw JSON →