最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何使用节点js运行python程序

运维笔记admin21浏览0评论

如何使用节点js运行python程序

如何使用节点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);
发布评论

评论列表(0)

  1. 暂无评论