python - 如何使用node js运行python程序

标签 python node.js express

我有一个 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 --图片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);

关于python - 如何使用node js运行python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54651642/

相关文章:

python - 在 Python 3 中使用带有多个文件的 gzip 存档

python - 将列表的列表解压缩到列表中

node.js - Node/Express 中的 BrowserHistory API

python - 同一应用程序中的 Openshift Python 和 Nodejs

node.js - 如何从云函数调用同步子函数

express - Express 4.0 中的路由器与应用程序

node.js - Node/Express 根据要求发送静态文件

python - python中如何避免全局变量和重复

python - 在 PyInstaller 中将数据与 .spec 文件捆绑

node.js - 如何在bluemix上配置socket.io粘性 session