Categories
JavaScript Answers

How to call a Python function from Node.js?

Spread the love

Sometimes, we want to call a Python function from Node.js.

In this article, we’ll look at how to call a Python function from Node.js.

How to call a Python function from Node.js?

To call a Python function from Node.js, we can call the spawn method.

For instance, we write

const {
  spawn
} = require("child_process");
const pythonProcess = spawn('python', ["path/to/script.py", arg1, arg2]);

to call the spawn method available in the child_process module.

We run python with the path to the script, arg1 and arg2 as the command line arguments.

The function we want to call should be in script.py.

Conclusion

To call a Python function from Node.js, we can call the spawn method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *