{"id":4784,"library":"stpyv8","title":"Python Wrapper for Google V8 Engine","description":"STPyV8 is a Python wrapper for the Google V8 JavaScript engine, enabling seamless interoperability between Python 3 and JavaScript. It serves as a modern fork of the original PyV8 project, continuously updated by Cloudflare to align with the latest Google V8 engine branches and Python 3 versions. The library receives frequent updates, often coinciding with new V8 releases, and provides pre-built wheels for various platforms.","status":"active","version":"13.1.201.22","language":"en","source_language":"en","source_url":"https://github.com/cloudflare/stpyv8","tags":["javascript","v8","engine","runtime","interoperability","python-javascript"],"install":[{"cmd":"pip install stpyv8","lang":"bash","label":"Install via pip"}],"dependencies":[],"imports":[{"symbol":"STPyV8","correct":"import STPyV8"},{"symbol":"JSContext","correct":"from STPyV8 import JSContext"},{"symbol":"JSClass","correct":"from STPyV8 import JSClass"}],"quickstart":{"code":"import STPyV8\n\nwith STPyV8.JSContext() as ctxt:\n    result = ctxt.eval(\"'Hello, ' + 'STPyV8!'\");\n    print(result)\n\n# Exporting a Python function to JavaScript\nclass MyFunctions(STPyV8.JSClass):\n    def multiply(self, a, b):\n        return a * b\n\nmy_funcs = MyFunctions()\nwith STPyV8.JSContext(my_funcs) as ctxt:\n    js_code = \"this.multiply(5, 3);\"\n    multiplication_result = ctxt.eval(js_code)\n    print(f\"Multiplication result: {multiplication_result}\")","lang":"python","description":"This example demonstrates how to initialize a V8 JavaScript context, evaluate a simple JavaScript expression, and export a Python class method to be callable from within the JavaScript context."},"warnings":[{"fix":"Ensure your Python environment is 3.9+ (e.g., `python3.9 -m pip install stpyv8`).","message":"STPyV8 requires Python 3.9 or newer for recent versions. Attempting to install or use with older Python versions may lead to build failures or runtime errors.","severity":"breaking","affected_versions":"<12.0.267.16"},{"fix":"Refer to the official GitHub repository for detailed build instructions specific to your operating system. For many users, `pip install stpyv8` will automatically fetch a pre-built wheel, avoiding source compilation.","message":"When building STPyV8 from source (e.g., if no pre-built wheel is available for your platform/Python version), you will need a C++ compiler (GCC/clang on Linux/macOS, MSVC on Windows), Python development headers, and potentially Boost libraries (though less common for recent versions).","severity":"gotcha","affected_versions":"All versions (when building from source)"},{"fix":"Manually copy `icudtl.dat` to the appropriate STPyV8 ICU data folder (e.g., `/usr/share/stpyv8` on Linux, `/Library/Application Support/STPyV8/` on macOS). This file is usually included in zip releases alongside the wheels if manual installation is needed.","message":"For complex JavaScript code, the underlying V8 engine may require an ICU data file (`icudtl.dat`). If this file is not correctly packaged with your STPyV8 installation (e.g., when building from source or with certain distribution methods), V8 operations can fail silently or with errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider bundling your JavaScript code using tools like Webpack or Rollup to produce a single, self-contained file without `import`/`export` statements. Alternatively, explore if V8's module loading capabilities can be exposed or configured within STPyV8's `JSContext`.","message":"STPyV8's direct `eval()` might not fully support modern JavaScript ES Modules (`import`/`export` syntax) out-of-the-box, potentially leading to `SyntaxError: Cannot use import statement outside a module`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Exercise caution when using STPyV8 in threaded applications. If possible, isolate V8 context creation and usage to a single thread. Design your application to minimize long-lived Python objects exposed to JavaScript or vice-versa to mitigate potential GC conflicts. Monitor GitHub issues for updates on threading stability.","message":"There are reported issues with STPyV8 in multi-threaded environments, including segmentation faults when used in separate threads. The presence of dual garbage collectors (Python and V8) can also lead to complexities and potential memory management issues in scenarios involving long-lived objects spanning both VMs.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}