简体   繁体   中英

node execFile python script in aws lambda

I am trying to execute a script through a node service hosted on AWS lambda, but consistently get a ENOENT exception.

2020-04-22 07:55:14.613 (-04:00)    9c8c54fc-2aa2-4d17-89d9-ca1e404191b7    ERROR   Error: spawn ./bin/test-bin.py ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
    at onErrorNT (internal/child_process.js:469:16)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ENOENT',
  code: 'ENOENT',
  syscall: 'spawn ./bin/test-bin.py',
  path: './bin/test-bin.py',
  spawnargs: [ 1, 2 ],
  cmd: './bin/test-bin.py 1 2'
}

Executing cat bin/test-bin.py in the child process spits out the source code of the script, ls -l through the child process shows that the script is executable, and the same code works locally on my linux machine.

  const { execFile } = require('child_process');
  execFile('cat', ["bin/test-bin.py"], (err, out) => {
    if (err) {
        console.error(err)
    }
    else {
      console.log(out)
    }
  });

The script:

#!/usr/bin/python

import sys
import time

def sum(n1, n2):
  return int(n1) + int(n2)

print(sum(sys.argv[1], sys.argv[2]))

Your python script is not a standalone executable - it relies on the shell interpreting the #!/usr/bin/python line at the top of the file to load the python interpreter ( /usr/bin/python ), and then run the script.

Because execFile doesn't load a shell, it won't do this. You could just use exec instead of execFile , but this is less safe, and slower.

Instead, run /usr/bin/python with execFile , with your script as an argument:

execFile('/usr/bin/python', ['bin/test-bin.py'], (err, out) => {
   // ...
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM