Python Wrapper for Google V8 Engine
Archived. The repository is read-only: no more fixes or releases will come.
Consider insteadpythonmonkeyPythie’s Index: 7288.1K downloads/moCompare →
pip install stpyv8STPyV8
STPyV8 allows interoperability between Python 3 and JavaScript running Google V8 engine. Use STPyV8 to embed JavaScript code directly into your Python project, or to call Python code from JavaScript.
STPyV8 is a fork of the original PyV8 project, with code changed to work with the latest Google V8 engine and Python 3. STPyV8 links with Google V8 built as a static library.
Currently the library builds on Linux, MacOS and Windows.
Usage Examples
Wrapping a JavaScript function in a Python function:
# simple.py
import STPyV8
with STPyV8.JSContext() as ctxt:
upcase = ctxt.eval("""
( (lowerString) => {
return lowerString.toUpperCase();
})
""")
print(upcase("hello world!"))
$ python simple.py
HELLO WORLD!
Using Python in V8
STPyV8 allows you to use Python functions, classes, and objects from within V8.
Exporting a Python class into V8 and using it from JavaScript:
# meaning.py
import STPyV8
class MyClass(STPyV8.JSClass):
def reallyComplexFunction(self, addme):
return 10 * 3 + addme
my_class = MyClass()
with STPyV8.JSContext(my_class) as ctxt:
meaning = ctxt.eval("this.reallyComplexFunction(2) + 10;")
print("The meaning of life: " + str(meaning))
$ python meaning.py
The meaning of life: 42
Using JavaScript in Python
STPyV8 allows you to use JavaScript functions, classes, object from Python.
Calling methods on a JavaScript class from Python code:
# circle.py
import STPyV8
with STPyV8.JSContext() as ctxt:
ctxt.eval("""
class Circle {
constructor(radius) {
this.radius = radius;
}
get area() {
return this.calcArea()
}
calcArea() {
return 3.14 * this.radius * this.radius;
}
}
""")
circle = ctxt.eval("new Circle(10)")
print("Area of the circle: " + str(circle.area))
$ python cicle.py
Area of the circle: 314
Find more in the tests directory.
Installing
STPyV8 is avaliable on PyPI (starting from release v12.0.267.16) and officially supports Python 3.9+
$ pip install stpyv8
Be aware that, starting from STPyV8 v12.0.267.14, installing boost-python and some other boost dependencies is not required anymore while it is still required if you're installing older versions (see later for details). Most Linux distributions and MacOS provide easy to install Boost packages and this is the suggested way to install the library in case you need it.