Run JavaScript code from Python
This library is no longer maintananced. Bugs are not be fixed (even if they are trivial or essential).
We suggest to use other library or to make a fork.
Run JavaScript code from Python.
PyExecJS is a porting of ExecJS from Ruby. PyExecJS automatically picks the best runtime available to evaluate your JavaScript program.
A short example:
::
>>> import execjs
>>> execjs.eval("'red yellow blue'.split(' ')")
['red', 'yellow', 'blue']
>>> ctx = execjs.compile("""
... function add(x, y) {
... return x + y;
... }
... """)
>>> ctx.call("add", 1, 2)
3
PyV8 <http://code.google.com/p/pyv8/>
__ - A python wrapper for
Google V8 engine,Node.js <http://nodejs.org/>
__PhantomJS <http://phantomjs.org/>
__Nashorn <http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/intro.html#sthref16>
__
Microsoft Windows Script Host <http://msdn.microsoft.com/en-us/library/9bbdkx3k.aspx>
__
(JScript)SlimerJS <http://slimerjs.org/>
__Mozilla SpiderMonkey <http://www.mozilla.org/js/spidermonkey/>
__::
$ pip install PyExecJS
or
::
$ easy_install PyExecJS
If EXECJS_RUNTIME
environment variable is specified, PyExecJS pick
the JavaScript runtime as a default:
::
>>> execjs.get().name # this value is depends on your environment.
>>> os.environ["EXECJS_RUNTIME"] = "Node"
>>> execjs.get().name
'Node.js (V8)'
You can choose JavaScript runtime by execjs.get()
:
::
>>> default = execjs.get() # the automatically picked runtime
>>> default.eval("1 + 2")
3
>>> import execjs.runtime_names
>>> jscript = execjs.get(execjs.runtime_names.JScript)
>>> jscript.eval("1 + 2")
3
>>> import execjs.runtime_names
>>> node = execjs.get(execjs.runtime_names.Node)
>>> node.eval("1 + 2")
3
The pros of PyExecJS is that you do not need take care of JavaScript environment. Especially, it works in Windows environment without installing extra libraries.
One of cons of PyExecJS is performance. PyExecJS communicate JavaScript runtime by text and it is slow. The other cons is that it does not fully support runtime specific features.
PyV8 <https://code.google.com/p/pyv8/>
__ might be better choice for
some use case.
Copyright (c) 2016 Omoto Kenji. Copyright (c) 2011 Sam Stephenson and Josh Peek. (As a author of ExecJS)
Released under the MIT license. See LICENSE
for details.
--print-available-runtimes
.--print-available-runtimes
fails in Python 2.7.cwd
argument.