Registry / serialization / js2py
library0.74pypypi✓ verified 21d ago

Js2Py is a pure Python library that acts as both a JavaScript to Python translator and a JavaScript interpreter. It allows Python developers to execute JavaScript code directly within their Python applications, or convert it to equivalent Python code. The current version is 0.74, and it sees active, though not strictly scheduled, development.

pip install js2py
INSTALL
IMPORT
SIG · JS2PY
J
js2py
serializationpythonv0.74
Install
4.3s avg
Import
982ms
Disk
36MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.74 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.616s · 32.5MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 4.3s · import 0.562s · 33MB
36MB installed
● package 36MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

js2py
import js2py
import js2py

This quickstart demonstrates how to evaluate a JavaScript code string and interact with defined functions, as well as directly evaluate simple expressions. `eval_js` returns a `PyJs` object or a Python equivalent of the JS return value.

import js2py # Define a JavaScript function js_code = """ function greet(name) { return 'Hello, ' + name + '!'; } """ # Evaluate the JavaScript code and get a Python callable object js_greet = js2py.eval_js(js_code) # Call the JavaScript function from Python result_func = js_greet('World') print(f"Function result: {result_func}") # You can also evaluate a simple expression directly direct_result = js2py.eval_js('10 * 5') print(f"Direct expression result: {direct_result}")
Debug
Known issues
gotchaJs2Py is a pure Python implementation and is not designed for high-performance JavaScript execution. It will be significantly slower than engines like V8 (Node.js/Chromium) or SpiderMonkey.
fix
Consider alternative solutions like `py_mini_racer` (V8 bindings) for performance-critical tasks, or Node.js subprocess calls.
affects: All
gotchaThe interpreter primarily supports ECMAScript 5.1 and a limited set of ECMAScript 6 (ES2015) features. Newer JavaScript syntax (ES2016+, async/await, modern modules, etc.) may not be supported or lead to unexpected behavior.
fix
Ensure your JavaScript code adheres to ES5.1 compatibility or thoroughly test for ES6 features you intend to use. Consult the project's GitHub for current compatibility details.
affects: All
gotchaType conversions between Python and JavaScript objects can be complex and sometimes lead to unexpected behavior. While basic types (numbers, strings, booleans, lists, dictionaries) are generally mapped, custom JS objects or complex Python objects may not translate as expected.
fix
Familiarize yourself with the 'Python and JavaScript object mapping' section in the Js2Py documentation. Test conversions thoroughly for critical data structures and object interactions.
affects: All
gotchaAs a pure Python library, Js2Py operates within the Python Global Interpreter Lock (GIL). Long-running JavaScript scripts will block the entire Python process, impacting concurrency for other Python threads.
fix
For long-running or CPU-bound JavaScript tasks, consider offloading them to separate processes using Python's `multiprocessing` module, or use asynchronous execution patterns if the JS itself is non-blocking.
affects: All
gotchaWhile Js2Py aims to provide a sandboxed environment for executing JavaScript, running arbitrary untrusted code always carries inherent security risks. A pure Python sandbox might have different vulnerabilities than a native V8 sandbox.
fix
Never execute JavaScript from untrusted sources without careful consideration and robust input validation. Even with sandboxing, exercise caution and minimize the attack surface if Js2Py is used in security-sensitive contexts.
affects: All
Upgrade
Version history
0.74latest on PyPI · released Nov 6, 2022
Audit
Dependencies
pyjsparserrequiredCore dependency for parsing JavaScript code.
sixrequiredUsed for Python 2/3 compatibility, though primarily used with Python 3 now.
Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
1
Resources