首页 > 解决方案 > JavaScript Spawn:如何在 Spawn python 脚本中将变量传递给 FLAG

问题描述

我有一个 python 脚本,它有两个 FLAG--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());});

我应该如何使它工作?先感谢您!

标签: javascriptpythonnode.jsspawn

解决方案


您可以像在 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]);

推荐阅读