2017-03-16 93 views
1

我写了下面的代码在app.js的Windows产卵子进程节点JS

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 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(); 
}); 

正如你可以看到蟒蛇的可执行文件的路径提供。此代码在Windows中正常工作,并在控制台上给出数组中数字的总和。我想在Ubuntu中执行相同的代码。我在Linux(Ubuntu)中为python可执行文件路径指定了什么?

回答

0

您可以使用process.platform获取当前平台。 例如在我的mac我有process.platform === 'darwin'documentation

1

这将取决于Linux设置中的python可执行文件的位置。

正如您所说这是一个Ubuntu(没有指定版本),您可以尝试使用python而不使用路径,因为python可执行文件应该已经在PATH中,并且通常会随您的发行版一起安装。

如果这样不起作用,可以在Linux shell上找出运行which python的python可执行文件路径。

最后,您还可以尝试/usr/bin/python作为可执行路径,因为它是它的常用位置。