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.