Skip to main content

Run JavaScript code from Python

Project description

PyExecJS

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

Supported runtimes

Installation

$ pip install PyExecJS

or

$ easy_install PyExecJS

Details

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
>>> jscript = execjs.get("JScript")
>>> jscript.eval("1 + 2")
3
>>> node = execjs.get("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 might be better choice for some use case.

License

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.

Changelog

1.3.0

  • Added cwd argument.

1.2.0

  • Supported Python 3.5

  • Supported Nashorn(Java 8 JavaScript engine) as runtime

  • Dropped support for Python 2.6 and 3.2

1.1.0

  • Supported Python 3.4

  • Supported SlimerJS as runtime

  • Supported PhantomJS as runtime

  • Fixed JScript runtime on Windows 8

1.0.5

  • Supported Python 3.3

  • Fixed file handle leaking

  • Fixed issue with passenger-nginx-4.0

1.0.4

  • Removed “import execjs” (it prevent execution of setup.py by Python 2.6)

1.0.3

  • Javascript sources were embeded in init.py. ‘which’ command were reimplemented by pure python.

1.0.2

  • Python 2.6.x was supported.

1.0.1

  • Forgotten shell=True was added to Popen.

1.0.0

  • First release.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

PyExecJS-1.3.1.zip (21.4 kB view details)

Uploaded Source

PyExecJS-1.3.1.tar.gz (13.8 kB view details)

Uploaded Source

File details

Details for the file PyExecJS-1.3.1.zip.

File metadata

  • Download URL: PyExecJS-1.3.1.zip
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyExecJS-1.3.1.zip
Algorithm Hash digest
SHA256 9db70c8b42e8e16f7a5f15e5431b6124eec39090c2fd93e3991842f223f36b5d
MD5 74556230847def92f2244e55af5b77d2
BLAKE2b-256 855e94dc03c0fa3667a36d20ec6fa97910049640fe6443e45369ef2e1ad4fa24

See more details on using hashes here.

File details

Details for the file PyExecJS-1.3.1.tar.gz.

File metadata

  • Download URL: PyExecJS-1.3.1.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for PyExecJS-1.3.1.tar.gz
Algorithm Hash digest
SHA256 8847acb0f177ba83e7b73d18c23f17a24146f1a197fe59242bca58d39e849bd4
MD5 cb64d118cc48af724088f35a2e9955aa
BLAKE2b-256 237185470f6def560e6897efe5e563a840caee22c6e5803183e8605d9212a036

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page