JavaScript Spawn:如何在 Spawn python 脚本中将变量传递给 FLAG

JavaScript Spawn: How to pass variable to FLAG in Spawn python script

我有一个 python 脚本,它有两个标志 --server--image

目前,在JavaScript中,我只能使用spawn将固定值分配给FLAGS。例如:(这确实会产生输出)

var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=./testImage/DSC00917.JPG']);

pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});

但是,我想分配一个字符串变量并将该字符串传递给FLAG。例如:(这是错误的,它不会产生任何输出)

var imagePath = './testImage/DSC00917.JPG'

var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=imagePath']);

pyProg.stdout.on('data', function (data) { console.log('This is result ' + data.toString());});

我应该如何让它发挥作用?提前致谢!

您可以像在 JavaScript 中的其他地方一样使用字符串连接。 如果您希望 console.log 打印一个变量,您可以这样做:

console.log('image path is ' + imagePath);

或者如果您使用的是 ES6 字符串插值:

console.log(`image path is ${imagePath}`);

同样适用于您的代码示例:

var imagePath = './testImage/DSC00917.JPG'
var pyProg = spawn('python', ['./MLmodel/inception_client.py', '--server=30.220.240.190:9000', '--image=' + imagePath]);