-4

I am trying to run the below script from Python.

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

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

emailAddress = 'my.email@company.com'
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.

halfer
  • 19,824
  • 17
  • 99
  • 186
ASH
  • 20,759
  • 19
  • 87
  • 200
  • Please provide the Python code. You'll have greater chances of getting a good response with [MRVE](https://stackoverflow.com/help/on-topic) – Lucas Wieloch Sep 10 '18 at 19:21
  • 1
    It appears that you are trying to run Javascript from the Python executable, Javascript is not valid Python syntax. You need to do much more than just `import execjs`. Note that `PyExecJS`, according to PyPi, is no longer maintained. – cdarke Sep 10 '18 at 19:25
  • Instead of going through a command-line interface I wanted to run this JS script in Python. Can I not do that? If not, it's totally fine. I thought this was do-able. – ASH Sep 10 '18 at 19:28
  • The examples from [PyExecJS](https://github.com/doloopwhile/PyExecJS) explicitly call `execjs.eval` or `execjs.compile` on JS code, they don't include JS code inline mixed with the Python. So, you have to do the same thing as those examples. – abarnert Sep 10 '18 at 19:55

1 Answers1

2

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 = 'my.email@company.com'
    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.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thanks a lot abarnert! When I ran the first script I got this: 'ProgramError: SyntaxError: Unexpected token var'. When I ran the second one, which I really prefer, I got this: 'ProgramError: SyntaxError: Unexpected token import'. Does that mean I can't pass in variables? That's what I was hoping to do. 'emailAddress' and 'apiKey' are variables. – ASH Sep 11 '18 at 19:04