在节点J中用python生成子进程

2024-05-28 20:09:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经在应用程序js对于Windows

在应用程序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();
});

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


Tags: thetopath应用程序datavarwindowsjs
2条回答

python中的可执行文件依赖于python中的可执行文件。在

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

如果这不起作用,您可以找出在linuxshell上运行which python的python可执行路径。在

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

你可以用流程.平台以获取当前平台。 例如在我的mac电脑上我有process.platform === 'darwin'documentation

相关问题 更多 >

    热门问题