{"id":4589,"library":"js2py","title":"Js2Py: JavaScript to Python Translator & Interpreter","description":"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.","status":"active","version":"0.74","language":"en","source_language":"en","source_url":"https://github.com/PiotrDabkowski/Js2Py","tags":["javascript","interpreter","transpiler","ecmascript","javascript-engine"],"install":[{"cmd":"pip install js2py","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Core dependency for parsing JavaScript code.","package":"pyjsparser","optional":false},{"reason":"Used for Python 2/3 compatibility, though primarily used with Python 3 now.","package":"six","optional":false}],"imports":[{"note":"Evaluates JavaScript code strings directly.","symbol":"eval_js","correct":"from js2py import eval_js"},{"note":"Translates JavaScript code into equivalent Python code.","symbol":"translate_js","correct":"from js2py import translate_js"}],"quickstart":{"code":"import js2py\n\n# Define a JavaScript function\njs_code = \"\"\"\nfunction greet(name) {\n    return 'Hello, ' + name + '!';\n}\n\"\"\"\n\n# Evaluate the JavaScript code and get a Python callable object\njs_greet = js2py.eval_js(js_code)\n\n# Call the JavaScript function from Python\nresult_func = js_greet('World')\nprint(f\"Function result: {result_func}\")\n\n# You can also evaluate a simple expression directly\ndirect_result = js2py.eval_js('10 * 5')\nprint(f\"Direct expression result: {direct_result}\")\n","lang":"python","description":"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."},"warnings":[{"fix":"Consider alternative solutions like `py_mini_racer` (V8 bindings) for performance-critical tasks, or Node.js subprocess calls.","message":"Js2Py 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"Type 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"As 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.","severity":"gotcha","affected_versions":"All"},{"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.","message":"While 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.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}