如何将NodeJs中的变量插入python脚本?

2024-04-29 04:07:05 发布

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

我使用python-shell在NodeJs环境中运行python脚本。在

我有以下NodeJs代码:

var PythonShell = require('python-shell');

var command = 'open1';
var comport = 6;

var options = {
    scriptPath: 'python/scripts'
};

PythonShell.run('controlLock.py', options, function (err, results) {
  if (err) throw err;
  console.log('results: %j', results);
});

在执行python controlLock脚本之前,我需要能够将command和COMPORT变量包含到脚本中(否则它将没有正确的值)。在

下面是controlLock.py文件

^{pr2}$

Tags: 代码py脚本varnodejsrequireshellresults
1条回答
网友
1楼 · 发布于 2024-04-29 04:07:05

你可以run the python script with arguments。传递给PythonShell.run()options有一个名为args的属性,可用于将参数传递给python脚本。然后您可以从python脚本中read these command line arguments,并在需要的地方插入它们。在

python-shell参数

var options = {
  scriptPath: 'python/scripts',
  args: [command, comport], // pass arguments to the script here
};

python脚本

^{pr2}$

相关问题 更多 >