0

Using node's child-spawn exec basically mimics the shell, so I should be able to do

exec('python' , (error, stdout, stderr) => { 
  if (error || stderr) {
    console.log('exec error ' , error )
    console.log('exec stderr ' ,  stderr)
  } else {
    console.log('exec output ' , stdout)
  }
})  

or

exec('python hello.py' , (error, stdout, stderr) => { 
  //same as above

but I get nothing back, not even errors.

What am I missing here ?

Thanks

1 Answer 1

2

python command expects some data to be written to stdin. You can write that data by using the subprocess object returned by exec:

const { exec } = require('child_process');

const subprocess = exec('python' , (error, stdout, stderr) => {
  if (error || stderr) {
    console.log('exec error ' , error )
    console.log('exec stderr ' ,  stderr)
  } else {
    console.log('exec output ' , stdout)
  }
})

subprocess.stdin.write('print("test");');
subprocess.stdin.end()

Probably hello.py also waits for some data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.