简体   繁体   English

从NodeJS执行python命令行工具

[英]Executing python command line tools from NodeJS

What would be the best way to execute command line tools from nodejs to python? 从nodejs到python执行命令行工具的最佳方法是什么? I'm working on a app that can generate blockchain certificates. 我正在开发可生成区块链证书的应用程序。

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

This is a python based command line tool, where you can generate blockchain certificates. 这是一个基于python的命令行工具,您可以在其中生成区块链证书。 I have followed the steps and everything is working fine in the virtual env. 我已经按照步骤操作,并且在虚拟环境中一切正常。 But now I want to know how to implement this in to a app, where I can run the command line tools external from NodeJS. 但是现在我想知道如何在应用程序中实现它,在这里我可以运行NodeJS外部的命令行工具。 Is this possible? 这可能吗? I have found some libraries where you can run python script from nodejs. 我发现了一些库,您可以在其中运行来自nodejs的python脚本。 Example I have used python shell. 示例我使用了python shell。 Whenever I run the setup script, I get missing file errors. 每当我运行安装脚本时,都会丢失文件错误。

Can someone guide me what the best way will be to solve this problem. 有人可以指导我解决该问题的最佳方法是什么。 Thanks in advance. 提前致谢。

You can use python shell in node, you would go about using it somewhat like the following: 您可以在节点中使用python shell ,可以像下面这样使用它:

var PythonShell = require('python-shell');

PythonShell.run('my_script.py', function (err) {
  if (err) throw err;
  console.log('finished');
});

for more documentation visit their github repository, hope i could help :) 有关更多文档,请访问他们的github存储库,希望我能帮到您:)

You should just be able to execute the commands using the child_process.exec() function. 您应该只能够使用child_process.exec()函数执行命令。

Make sure to only use the file once the python script has finished. 确保仅在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
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM