Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
998 views
in Technique[技术] by (71.8m points)

Trying to run JavaScript from Python

I am trying to run the below script from Python.

import execjs
var request = require('request');

var apiHostName='https:/url.com';

emailAddress = '[email protected]'
apiKey = 'api_key'

function callback(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log("Identity with email address " + emailAddress + " found:");
    var b= JSON.parse(body);
    console.log("id="+b.identityId+",api key="+b.apiKey+",type="+b.type);
  } else{
    if (response.statusCode == 401) {
      console.log ("Couldn't recognize api key="+apiKey);
    } else if (response.statusCode == 403) {
      console.log ("Operation forbidden for api key="+apiKey);
    } else if (response.statusCode == 404) {
      console.log ("Email address " +emailAddress + " not found");
    }
  }
}

I did this:

pip install py-mini-racer
pip install PyExecJS

I would think this is pretty close, based on the research that I did, but I don't know for sure. All I'm getting now is this error: 'SyntaxError: invalid syntax'

The error occurs on this line: 'var request = require('request');'

Obviously, I am using my actual email and api key. I am running Python 3.x.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, you're using a library, PyExecJS, which claims to be both no longer maintained, and poorly designed.

So, this probably isn't the best choice in the first place.


Second, you're using it wrong.

The examples all include JS code as strings, which are passed to execjs.eval or execjs.compile.

You're trying to include JS code directly inline as if it were Python code. That isn't going to work; it will try to parse the JS code as Python and raise a SyntaxError because they aren't the same language.1

So, you have to do the same thing as the examples. That might look something like this:

import execjs

jscode = """
    var request = require('request');

    var apiHostName='https:/url.com';

    emailAddress = '[email protected]'
    apiKey = 'api_key'

    function callback(error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log("Identity with email address " + emailAddress + " found:");
        var b= JSON.parse(body);
        console.log("id="+b.identityId+",api key="+b.apiKey+",type="+b.type);
      } else{
        if (response.statusCode == 401) {
          console.log ("Couldn't recognize api key="+apiKey);
        } else if (response.statusCode == 403) {
          console.log ("Operation forbidden for api key="+apiKey);
        } else if (response.statusCode == 404) {
          console.log ("Email address " +emailAddress + " not found");
        }
      }
    }
"""
execjs.eval(jscode)

Or, probably even better, move the JavaScript to a separate .js file, then run it like this:

import os.path
import execjs

dir = os.path.dirname(__file__)
with open(os.path.join(dir, 'myscript.js')) as f:
    jscode = f.read()
execjs.eval(jscode)

1. Someone could write an import hook for Python that did something similar to perl's Inline::Python, Inline::Java, etc. for Python, allowing you to embed code from other languages directly in your Python scripts. Periodically, someone does try to write such a thing, but they always seem to abandon it as a bad idea before it's even production-ready, or redesign it to be more like PyExecJS.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...