4

I created some code to use a python function from a node.js backend. When running it on my ubuntu computer, it works - however! When running the code on his windows machine it gives this stacktrace.

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn python 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:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622: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:622:3)

This is the node.js file

const spawn = require("child_process").spawn;
const pythonProcess = exec('python',["./script.py", 2, 4]);

pythonProcess.stdout.on('data', function(data) {
    console.log(data.toString('utf-8'))
} )

and this is the script.py file

import sys

print("work with me please")
sys.stdout.flush()

There is a ton of people with issues like this, however all the answers seem to be over specific for the particular person. Some mentions path variables, some npm.cmd and others something else entirely.

How should I work around this particular case?

I have tried npm init, npm install, moving pieces of code around, googling and changed scope of cmd and directory and so on.

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Jonas Grønbek
  • 1,709
  • 2
  • 22
  • 49
  • 1
    Have you verified that calling `python ./script.py` works when called from the command line on the Windows machine? – Chase Mar 06 '19 at 23:20
  • @Chase yes sir! – Jonas Grønbek Mar 06 '19 at 23:58
  • 1
    This is a black hole that is easy to fall into but another obvious thing to check, was the Node code on the Windows machine simply copied over and run or was a fresh version of node_modules installed with `npm install`? Cross-OS and cross-architecture usually means some modules need to be compiled on install for the system they are on. – Chase Mar 07 '19 at 00:30
  • @Chase I should have added it to the post. Yes we npm init installed, deleted chunks of code put back in and that kind of fiddling for a couple of hours before deciding to post this question. So no, if only it was the case! – Jonas Grønbek Mar 07 '19 at 01:55
  • @Chase, I did you wrong. We went back and reinstalled python and it worked. You are the man of the hour! – Jonas Grønbek Mar 07 '19 at 16:00

2 Answers2

5

Hy I had similar error:

events.js:292 throw er; // Unhandled 'error' event ^

Error: spawn python 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) Emitted 'error' event on ChildProcess instance at: at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12) at onErrorNT (internal/child_process.js:469:16) at processTicksAndRejections (internal/process/task_queues.js:84:21) { errno: 'ENOENT', code: 'ENOENT', syscall: 'spawn python', path: 'python', spawnargs: [ '/home/NodeJsRunPython/script2.py' ] }

for this script adapted from https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f

I change 'python' in 'python3' on

const python = spawn('python3', [__dirname +'/script2.py']);

For me it works:

const express = require('express')
const {spawn} = require('child_process');
const app = express()
const port = 3000

app.get('/', (req, res) => {
 
 var dataToSend;
 // spawn new child process to call the python script
 const python = spawn('python', [__dirname +'/script2.py']);
 // collect data from script
  python.stdout.on('data', function (data) {
  console.log('Pipe data from python script ...');
  dataToSend = data.toString();
 });
 // in close event we are sure that stream from child process is closed
 python.on('close', (code) => {
 console.log(`child process close all stdio with code ${code}`);
 // send data to browser
 res.send(dataToSend)
 });
 
})
app.listen(port, () => console.log(`Example app listening on port 
${port}!`))
Constantin
  • 61
  • 1
  • 2
3

Hey i had a similar problem and this did the trick , it was fixed when i added pythonPath: 'python' :

const { PythonShell } = require('python-shell');

let options = {
    mode: 'text',
    pythonPath: 'python',
    pythonOptions: ['-u'], // get print results in real-time
    scriptPath: 'path',
    args: ['arg1', 'arg2']
};

PythonShell.run('scraper.py', options, function(err, results) {
    if (err) console.log(err);
    // results is an array consisting of messages collected during execution
    console.log('results: %j', results);
});
omn_1
  • 345
  • 2
  • 14