简体   繁体   中英

Executing python command line tools from NodeJS

What would be the best way to execute command line tools from nodejs to python? I'm working on a app that can generate blockchain certificates.

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

This is a python based command line tool, where you can generate blockchain certificates. 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. Is this possible? I have found some libraries where you can run python script from nodejs. Example I have used 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:

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 :)

You should just be able to execute the commands using the child_process.exec() function.

Make sure to only use the file once the python script has finished.

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
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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