我寫了下面的代碼在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可執行文件路徑指定了什麼?