无法使用 node exec 执行 python
Cannot execute python with node exec
使用node的child-spawn
exec
基本上模仿了shell,所以我应该可以做到
exec('python' , (error, stdout, stderr) => {
if (error || stderr) {
console.log('exec error ' , error )
console.log('exec stderr ' , stderr)
} else {
console.log('exec output ' , stdout)
}
})
或
exec('python hello.py' , (error, stdout, stderr) => {
//same as above
但我什么也没得到,甚至没有错误。
我在这里错过了什么?
谢谢
python
命令需要将一些数据写入标准输入。您可以使用 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()
可能hello.py
也在等待一些数据。
使用node的child-spawn
exec
基本上模仿了shell,所以我应该可以做到
exec('python' , (error, stdout, stderr) => {
if (error || stderr) {
console.log('exec error ' , error )
console.log('exec stderr ' , stderr)
} else {
console.log('exec output ' , stdout)
}
})
或
exec('python hello.py' , (error, stdout, stderr) => {
//same as above
但我什么也没得到,甚至没有错误。
我在这里错过了什么?
谢谢
python
命令需要将一些数据写入标准输入。您可以使用 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()
可能hello.py
也在等待一些数据。