Javascript 在节点JS中用python生成子进程
我在app.js for Windows中编写了以下代码 app.jsJavascript 在节点JS中用python生成子进程,javascript,python,node.js,ubuntu,Javascript,Python,Node.js,Ubuntu,我在app.js for Windows中编写了以下代码 app.js app.route('/file').post(function (req,res,next) { // The path to your python script var myPythonScript = "script.py"; // Provide the path of the python executable, if python is available as environment variable the
app.route('/file').post(function (req,res,next) {
// The path to your python script
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 path =resolve("C:\\Python27\\python.exe");
var pythonExecutable = path;
var macadd =req.body.macadd;
var percent =req.body.percent;
// 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(String.fromCharCode.apply(null, data));
});
var data = JSON.stringify([1,2,3,4,5]);
scriptExecution.stdin.write(data);
// End data write
scriptExecution.stdin.end();
});
如您所见,提供了python可执行文件的路径。此代码在Windows中正常工作,并在控制台上给出数组中数字的总和。我想在Ubuntu中执行相同的代码。我应该为Linux(Ubuntu)中的python可执行文件路径指定什么?您可以使用process.platform获取当前平台。 例如,在我的mac电脑上,我有
process.platform==='darwin'
。
这将取决于python可执行文件在Linux设置中的位置 正如您所说的,这是一个Ubuntu(没有指定版本),您可以尝试不使用路径而直接使用
python
,因为python可执行文件应该已经在路径中,并且通常随发行版一起安装
如果这不起作用,您可以找出在Linux shell上运行的python可执行路径which python
最后,您还可以尝试使用可执行路径/usr/bin/python
,因为它是可执行路径的常用位置