K
K
kochkaqqq2020-05-21 02:32:40
Python
kochkaqqq, 2020-05-21 02:32:40

How to run py script from js file?

Are there any ways to execute a py (python) file from a js (node.js) file?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2020-05-21
@kochkaqqq

test.py.js

const { exec } = require('child_process');
exec('python test.py', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  console.log(`stdout: ${stdout}`);
  console.error(`stderr: ${stderr}`);
});

test.py
count = 0
while count < 1e6:
    count = count + 1
print(count)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question