如何使用节点js运行python程序
我有一个Express Node.js应用程序,但我想使用以下命令运行机器学习python codw。
我已经找到一些运行python机器学习代码的在线解决方案。使用此stackoverflow链接How to call a Python function from Node.js
但是通过这个解决方案,我只运行一个python代码。
var spawn = require("child_process").spawn;
var process = spawn('python', ["./my_script.py"]);
process.stdout.on('data', function (data) {
res.send(data.toString());
})
使用此代码,我只运行代码python hello.py
。但我想运行以下命令的代码python test.py --model dataset/test.model --image s.jpg
你可以使用python-shell
import {PythonShell} from 'python-shell';
const options = {
args: [
'--image',
's.jpg'
]
};
PythonShell.run('script.py', options, (err, results) => {
// your code
});
如果你想使用spawn,你可以这样做:
const args = ['script.py', '--image', 's.jpg'];
const process = spawn('python', args);