2

I was trying to run some python script from electron app in windows 10.The sample code that I am trying to run is:

let {PythonShell} = require('python-shell')
PythonShell.run('test.py',  function  (err, results)  {
 if  (err)  throw err;
 console.log('test.py finished.');
 console.log('results', results);
});

The code is expected to run test.py which contains a simple print statement.But the terminal shows the following error log:

throw er; // Unhandled 'error' event
      ^

Error: spawn py ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
  • Possible duplicate of [How do I debug "Error: spawn ENOENT" on node.js?](https://stackoverflow.com/questions/27688804/how-do-i-debug-error-spawn-enoent-on-node-js) – Bilal Siddiqui Oct 15 '19 at 09:01

1 Answers1

2

Looks like the Python executable is not available from your node script, which is almost always the case in Windows. Either add the python executable to your PATH variable, or specify the executable path in the options. See constructor options for more details.

Use https://github.com/extrabacon/python-shell#pythonshellscript-options-constructor

Set pythonPath & try.

let options = {
    pythonPath: 'C:\\python27\\python',
  };

  let {PythonShell} = require('python-shell')
  PythonShell.run('test.py', options,  function  (err, results)  {
   if  (err)  throw err;
   console.log('test.py finished.');
   console.log('results', results);
  });

https://github.com/extrabacon/python-shell/issues/3#issuecomment-52174564

Sudhakar Ramasamy
  • 1,608
  • 1
  • 6
  • 18