I am trying to call a python script using node.js child process.
JS
console.log('Hello World!')
const path = require('path')
const {spawn} = require('child_process')
/**
* Run python script, pass in `-u` to not buffer console output
* @return {ChildProcess}
*/
function runScript(){
return spawn('python', [
"-u",
path.join(__dirname, 'pythonscript.py'),
"--foo", "some value for foo",
]);
}
const subprocess = runScript()
// print output of script
subprocess.stdout.on('data', (data) => {
console.log(`data:${data}`);
});
subprocess.stderr.on('data', (data) => {
console.log(`error:${data}`);
});
subprocess.stderr.on('close', () => {
console.log("Closed");
});
python
mydict = {
"brand": "Ford",
"model": "Mustang",
"licensePlate" : "O XXX YYY",
"year" : 1964,
"insured": {
'lastname' : 'Snow',
'firstname' : 'John',
'adress' : 'Holiday street, 4 - Paradise'
}
}
print(mydict)
The purpose is to be able to use the dictionary created by the python code in javascript.
But a this step I am faced to an error which is provided here below.
Hello World!
events.js:291
throw er; // Unhandled 'error' event
^
Error: spawn python ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:268:19)
at onErrorNT (internal/child_process.js:470: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:274:12)
at onErrorNT (internal/child_process.js:470:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ENOENT',
code: 'ENOENT',
syscall: 'spawn python',
path: 'python',
spawnargs: [
'-u',
'/mnt/c/Users/xxxx/javascript/pythonscript.py',
'--foo',
'some value for foo'
]
}
can you help me with this? I work with Ubuntu Bash for Windows 10, I don't know if this information is relevant or not...