Skip to content
Related Articles
Open in App
Not now

Related Articles

Pass function and arguments from node.js to Python

Improve Article
Save Article
  • Last Updated : 04 Sep, 2022
Improve Article
Save Article

Prerequisites: How to run python scripts in node.js using the child_process module.

In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process.

Although Node.js is one of the most widely used web development frameworks, it lacks machine learning, deep learning, and artificial intelligence libraries. Python, fortunately, supports all of these and many more. Fortunately, those who are unfamiliar with the Django Framework but who utilize the Node.js framework can run Python using the child_process module for Node.js. 

The Node.js Child Process module allows us to run scripts or commands written in languages other than JavaScript (like Python, Java, and C++). Machine learning algorithms, deep learning algorithms, and many other capabilities provided by the Python library may be integrated into a Node.js application. We can use Child Process to execute Python scripts in Node.js applications and feed data into and out of Python scripts.

As you might be aware, what child_process basically does is sort of under the hood run the command you provide it with, along with the command line arguments. We can use this very fact to pass functions and arguments from node.js to python.

Syntax: python3 -c <command>


So basically there is a -c option that can be used to directly run commands provided as a string without having to create a python file.

python3 -c "print('Hello World');"


How to pass function and arguments from node to python, using child_process?


Stepwise Implementation

Step 1: Here we have created a file name, whose job is to output a gender specific compliment as soon as a call to compliment.giveMe() is made. It takes a Boolean argument isHe which is used to identify the gender of the person asking for the compliment. The following is the code:


import random
def output(compliments):
def giveMe(isHe):
    she = [
        "I miss your grin.",
        "You're an incredible buddy.",
        "I can't believe I met someone like you.",
        "I feel thrilled every time I see you.",
        "I adore making you laugh.",
        "You're my greatest friend.",
        "I'll always have your back.",
        "I adore every inch of you— even your toes.",
        "You have a seductive personality.",
        "You're my queen.",
        "I miss you even when you haven't left yet.",
        "I feel happy to be with you.",
        "You always know how to make me feel at home.",
        "I can totally be myself with you," "I love your style.",
        "I love your style.",
    he = [
        "What did you do to become in such great shape?\
        I'm interested in learning more about your training\
        "I like men who take care of themselves",
        "Wow, you're extremely gorgeous!",
        "What sort of deodorant are you wearing today? It smells\
        fantastic! ",
        "Your eyes have a nice look to them. I find it simple to\
        become lost in them. It really makes you look like you have\
        something deep to think about.",
        "Have you experimented with your hair? It appeals to me much.",
        "What sort of shoes are you wearing today? You should wear\
        it like that more often!",
        "Your posture is quite straight, which I admire. That must \
        have taken a long time for you to perfect ",
        "I'm quite impressed with how well you look after your health\
        . What do you do to keep your work-life balance?",
        "I admire how gentlemanly you are at all times. You are living\
        proof that chivalry still exists ",
        "You're very amusing. Your wonderful sense of humour is always\
        appreciated ",
        "I appreciate your thoughtfulness. Finding a man who is kind\
        might be challenging ",
        "I believe you comprehend what I'm saying. In the past, I've\
        had difficulty finding folks who comprehend what I'm saying \
        and thinking. I appreciate you always taking the time to learn\
        about me.",
        "I'm blown away by how composed you are in stressful situations\
        . It makes working through difficult situations a lot simpler. ",
        "You're incredibly good at active listening. I can always tell \
        whether you're paying attention to what I'm saying. That is \
        very appreciated ",
    if isHe:

Step 2: Here, we have created a file named index.js which is used to provide the Boolean argument to call the function compliment.giveMe() from node.js. to run the code.


import {spawn} from "child_process";
let argument = "True";
const pythonProcess = spawn('python3', ['-c'
`import compliment; compliment.giveMe(${argument});`]);
pythonProcess.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
pythonProcess.stderr.on('data', (data) => {
    console.log(`stderr: ${data}`);
pythonProcess.on('exit', (code) => {
    console.log(`Python process ended with code: ${code}`);


How to pass function and arguments from node to python, using child_process?


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!