【问题标题】:Executing python command line tools from NodeJS从 NodeJS 执行 python 命令行工具
【发布时间】:2018-12-01 18:22:46
【问题描述】:

从 nodejs 到 python 执行命令行工具的最佳方式是什么?我正在开发一个可以生成区块链证书的应用程序。

https://github.com/blockchain-certificates/cert-tools

这是一个基于 python 的命令行工具,您可以在其中生成区块链证书。我已按照步骤操作,并且在虚拟环境中一切正常。但现在我想知道如何在应用程序中实现这一点,我可以在其中运行 NodeJS 外部的命令行工具。这可能吗?我找到了一些库,您可以在其中从 nodejs 运行 python 脚本。示例我使用了 python shell。每当我运行安装脚本时,都会出现丢失文件错误。

有人可以指导我解决这个问题的最佳方法是什么。提前致谢。

【问题讨论】:

    标签: javascript python node.js command


    【解决方案1】:

    你可以在节点中使用python shell,你可以像下面这样使用它:

    var PythonShell = require('python-shell');
    
    PythonShell.run('my_script.py', function (err) {
      if (err) throw err;
      console.log('finished');
    });
    

    有关更多文档,请访问他们的 github 存储库,希望我能提供帮助 :)

    【讨论】:

      【解决方案2】:

      您应该能够使用child_process.exec() 函数执行命令。

      确保仅在 python 脚本完成后才使用该文件。

      child_process.exec('py path/to/script.py arg1 arg2', (err, stdout, stderr) => {
        if (err) {
          console.error(err);
          return;
        }
        console.log(stdout);
        // check if generated file exists
        let fileExists = require("fs").existsSync("/path/to/generated/file");
        if (fileExists) {
          // do something with the file
        }
      });
      

      【讨论】:

        猜你喜欢
        • 2011-02-24
        • 2017-07-04
        • 2020-11-07
        • 1970-01-01
        • 2020-08-22
        • 2017-11-01
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多