1

I am trying to call a python script as a child process within a node script. The output of the script is to be used within a callback. The code looks like this:

//myFunction.js

const myFunction = callback => {
  let py = process.spawn('python', ['../folder/pyscript.py'], {
    cwd: '../folder/'
  });

  let str = '';

  py.stdout.on('data', data => {
    str += data.toString();
  }

  py.stdout.on('end', () => {
    callback(str);
  }
}

exports.myFunction = myFunction;

This code works as expected when I directly run node myFunction.js (with an instance of myFunction within the script) and it works fine when I require the module in any other files within the same directory as myFunction.js.

But it fails with the following error when the module is required in a different higher level directory:

error: spawn python ENOENT

I'm guessing this has something to do with paths (value of cwd maybe?) but I can't seem to fix this. I've looked up similar questions but the answers aren't helping.

Any help will be appreciated. :)

blackgreen
  • 34,072
  • 23
  • 111
  • 129
Ajmal
  • 107
  • 1
  • 12
  • 1
    `../folder/pyscript.py` is a relative path, of course it depends on the correct cwd. Did you try an absolute path? – Vasiliy Faronov Aug 06 '17 at 13:39
  • I think I missed your comment back when I asked this, but you may be correct. I'll add an answer to my own question in order to help any future askers. Thanks! :) – Ajmal Nov 28 '18 at 10:11
  • This worked for me: https://stackoverflow.com/a/65008091/8119511 – Ank_247shbm Mar 03 '22 at 19:20

4 Answers4

0

Apparently, the issue is with the cwd. Everything in the script is relative to the path of the directory from where the script is invoked. So basically, running node myFunction.js from the project root directory (say ~/projects/myProject would set the cwd to ~/projects/myProject/../folder which would evaluate to ~/projects/folder. This is obviously incorrect, since in all probability, no directory named folder exists on the system, and thus this would lead to an ENOENT error.

The solution would be to construct the absolute path of your script in the code, perhaps by using the __dirname__ property in combination with the functionalities provided by the path module.

Ajmal
  • 107
  • 1
  • 12
0

I struggled with this issue for days, before realizing that my script file was not getting picked up and spawned by nodeJS, because of some filepath issue.

Although I don't guarantee this will work for everyone, depending on their setup, this is what I did in my nodejs file:

let py = process.spawn('python', [__dirname + '../folder/pyscript.py']);

As you can see, I didn't have to use the {cwd: '../folder/'} option.

If your script is in the current directory as your javascript file, just do

let py = process.spawn('python', [__dirname + './pyscript.py']);  

I should also point out that:

process.spawn('python', ['./pyscript.py']);

never worked for me and I spent days wondering why. Could find an answer until I tried this technique. Hope someone having this issue find this answer useful.

AllJs
  • 1,760
  • 4
  • 27
  • 48
0

Using ${process.cwd()} worked for me... you can write it like this

let py = process.spawn('python', [`${process.cwd()}/folder/pyscript.py`]});
0

If you are using a ubuntu machine then probably we have only python3 is added to the environment variables by default. Try to change the process name from python to python3

let py = process.spawn('python', [__dirname + './pyscript.py']);  

to

let py = process.spawn('python3', [__dirname + './pyscript.py']);  
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81