javascript 使用 node.js child_process 调用 python 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34213845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call python script using node.js child_process
提问by Bing Gan
I was trying to call a python code from my node file.
我试图从我的节点文件中调用 python 代码。
Here is my node.js Code:
这是我的 node.js 代码:
var util = require("util");
var spawn = require("child_process").spawn;
var process = spawn('python',["workpad.py"]);
util.log('readingin')
process.stdout.on('data',function(data){
util.log(data);
});
and my python part:
和我的python部分:
import sys
data = "test"
print(data)
sys.stdout.flush()
In the cmd window, only util.log('readingin')
is shown. What's the problem of my code?
在 cmd 窗口中,只util.log('readingin')
显示 。我的代码有什么问题?
回答by Scott Stensland
There is no problem ...
没有问题 ...
Here is a slight tweak of your working code (I convert the buffer to string so its human readable)
这是您的工作代码的轻微调整(我将缓冲区转换为字符串,以便人类可读)
// spawn_python.js
var util = require("util");
var spawn = require("child_process").spawn;
var process = spawn('python',["python_launched_from_nodejs.py"]);
util.log('readingin')
process.stdout.on('data',function(chunk){
var textChunk = chunk.toString('utf8');// buffer to string
util.log(textChunk);
});
and here is your python
这是你的蟒蛇
# python_launched_from_nodejs.py
import sys
data = "this began life in python"
print(data)
sys.stdout.flush()
finally here is output of a run
最后这里是运行的输出
node spawn_python.js
11 Dec 00:06:17 - readingin
11 Dec 00:06:17 - this began life in python
node --version
节点 --version
v5.2.0
v5.2.0
回答by shekhar Kumar
I was also facing the same issue, I found this:
我也遇到了同样的问题,我发现了这个:
var myPythonScript = "script.py";
// Provide the path of the python executable, if python is available as
// environment variable then you can use only "python"
var pythonExecutable = "python.exe";
// Function to convert an Uint8Array to a string
var uint8arrayToString = function(data){
return String.fromCharCode.apply(null, data);
};
const spawn = require('child_process').spawn;
const scriptExecution = spawn(pythonExecutable, [myPythonScript]);
// Handle normal output
scriptExecution.stdout.on('data', (data) => {
console.log(uint8arrayToString(data));
});
// Handle error output
scriptExecution.stderr.on('data', (data) => {
// As said before, convert the Uint8Array to a readable string.
console.log(uint8arrayToString(data));
});
scriptExecution.on('exit', (code) => {
console.log("Process quit with code : " + code);
});
回答by MikeZhang
Your python code is not correct:
你的python代码不正确:
import sys
data = "test"
print(data) ###not test
sys.stdout.flush()
回答by user2263572
You could always try something like this:
你总是可以尝试这样的事情:
var child_process = require('child_process');
child_process.exec('python myPythonScript.py', function (err){
if (err) {
console.log("child processes failed with error code: " + err.code);
}
});