首页 > 解决方案 > 从nodejs在anaconda环境中调用带有args的python文件

问题描述

我正在使用以下参数调用特定文件 main.py:

main.py --train --num_epoch 100 --id 93

在“gangan”的conda环境中

以下是我的代码:

const express = require('express')
const {spawn} = require('child_process');
const app = express()
const port = 3000
app.get('/', (req, res) => {
      var dataToSend;
      // spawn new child process to call the python script
      const environmentName = 'gangan';
      const pythonScript = './synthetic-time-series-smart-grid-master/main.py';
      const command = `conda run -n ${environmentName} python ${pythonScript}`;
      const python = spawn(command, { shell: true });
      // collect data from script
      python.stdout.on('data', function (data) {
             console.log('Pipe data from python script ...');
             dataToSend = data.toString();
      });
      // in close event we are sure that stream from child process is closed
      python.on('close', (code) => {
      console.log(`child process close all stdio with code ${code}`);
      // send data to browser
      res.send(dataToSend)
      });

 })
 app.listen(port, () => console.log(`Example app listening on port ${port}!`))

我运行时得到的唯一输出是 在 HTMl中输入图像描述。

这是程序通常执行的方式 在此处输入图像描述

标签: javascriptpythonnode.jsapiconda

解决方案


推荐阅读