python-quickjs-runtime
A lightweight Python binding for the QuickJS Javascript Engine.
This library allows you to execute JavaScript code within your Python applications, providing seamless interoperability between Python and JavaScript.
Features
- Evaluate JavaScript: Run JS code strings directly from Python.
- ES2023 Support: Support for modern JS features including
async/await. - Data Exchange: Pass
int,float,str,bool,list,dict, andBigIntbetween Python and JS. - Python Callbacks: Call Python functions directly from JavaScript.
- REPL: Includes a simple interactive shell for testing JS snippets.
- Easy Build: Uses
zigcc-buildfor reliable cross-platform compilation.
Installation
You can install the package from the source:
pip install .
Usage
Basic Evaluation
from quickjs_runtime import Runtime
rt = Runtime()
ctx = rt.new_context()
# Evaluate simple expressions
result = ctx.eval("1 + 2")
print(result) # 3
# Evaluate strings
greeting = ctx.eval("'Hello ' + 'World'")
print(greeting) # "Hello World"
Interoperability
You can set global variables in the JavaScript context and retrieve results.
from quickjs_runtime import Runtime
rt = Runtime()
ctx = rt.new_context()
# Set variables
ctx.set("x", 10)
ctx.set("y", 20)
# Use them in JS
result = ctx.eval("x * y")
print(result) # 200
Calling Python from JavaScript
You can pass Python functions to the JavaScript context.
from quickjs_runtime import Runtime
def add(a, b):
return a + b
rt = Runtime()
ctx = rt.new_context()
ctx.set("py_add", add)
# Call the Python function from JS
result = ctx.eval("py_add(5, 7)")
print(result) # 12
Custom Filenames and Stack Traces
You can provide a filename to eval for better error messages.
from quickjs_runtime import Runtime
rt = Runtime()
ctx = rt.new_context()
try:
ctx.eval("throw new Error('oops')", filename="my_script.js")
except RuntimeError as e:
print(e) # error message containing 'my_script.js'
Async/Await Support
Wait for Promises to settle using eval_sync.
from quickjs_runtime import Runtime
rt = Runtime()
ctx = rt.new_context()
script = """
async function fetchData() {
return "done";
}
fetchData();
"""
# eval_sync will run the job queue until all promises are settled
ctx.eval_sync(script)
REPL
The package includes a simple REPL (Read-Eval-Print Loop).
python -m quickjs_runtime
Development
This project uses zigcc-build to compile the C extensions. This ensures a consistent build environment without needing to install system-wide C compilers.
To build and install locally:
python -m build
pip install dist/*.whl
To run tests:
pytest
Metadata
Release files for quickjs-runtime 0.2.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| quickjs_runtime-0.2.6.tar.gz | 13.2 kB | Without provenance | Details for quickjs_runtime-0.2.6.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| quickjs_runtime-0.2.6-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details for quickjs_runtime-0.2.6-cp312-cp312-win_amd64.whl |
| quickjs_runtime-0.2.6-cp312-cp312-macosx_15_0_arm64.whl | CPython 3.12 | CPython 3.12 | macOS 15.0+ ARM64 | Details for quickjs_runtime-0.2.6-cp312-cp312-macosx_15_0_arm64.whl |
Total release size: 3.2 MB
Release files / quickjs_runtime-0.2.6.tar.gz
| Download URL | quickjs_runtime-0.2.6.tar.gz |
|---|---|
| Size | 13.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db0f1f991cf8b57b102a551b379bf219abc00af791df37a20b978bc35d150f5f
|
|
BLAKE2b-256 checksum How to use checksums |
d113e2cea1812f6d3b5aea2d6b9e2b67544b8652157e65493701f2eed164ed4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / quickjs_runtime-0.2.6-cp312-cp312-win_amd64.whl
| Download URL | quickjs_runtime-0.2.6-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 1.5 MB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
4022a837e18be7158a285d3e3d82d216423b86c3a97874fa87adb99c2474e97c
|
|
BLAKE2b-256 checksum How to use checksums |
8d86c95cd03ce4d88e1129b2f6b8fa3aed5cf00df101afc5434f73b83df1cb2a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / quickjs_runtime-0.2.6-cp312-cp312-macosx_15_0_arm64.whl
| Download URL | quickjs_runtime-0.2.6-cp312-cp312-macosx_15_0_arm64.whl |
|---|---|
| Size | 1.7 MB |
| Tags | CPython 3.12 macOS 15.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
18162c4a912f0f0f517febb345e2f2389c2e82838d16bf8dd21bf1bebb8c2ce1
|
|
BLAKE2b-256 checksum How to use checksums |
f67c5f3675cbc370de32a345ed6279be261121a91faa41e01ebea9961e6ae5ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Security for quickjs-runtime 0.2.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.