So you have come across a situation where in you want to call a Node.js script from your Python code. But how do we go about doing that? Is there any package that we need to install in Python or Node.js that will help us with this? Or can we do it directly using basic Python and Node.js programming? What is the standard approach to solve this?
We will answer all the above questions in this article today. So, are you are ready to learn about it now? Cool, let us get started right away!
Setup Required To Call Node.js Script From Python
So to begin with, we will first take a look at what all needs to be installed in our computer to get this going. Alright?
There are several tutorials available on the internet that can guide you in how to install Node.js on your computer. But let me give you a gist of the steps usually involved for a Linux computer:
Step 1 – Install Node Package Manager (NPM)
You can do this by running the command:
sudo apt install npm
Step 2 – Check which version of Node.js is installed
You can do this by running the command:
That is it! It is as simple as that!
So with Node.js installation out of the way, we can now see how to call a Node.js script from a Python file.
To begin with, let us create a simple Node.js file that simply prints “Hello, World!” on our screen. So here is how the file looks like:
Save this as hello.js in your computer directory. To test that this file works, simply call the command:
If the above command prints the “Hello, World!” string on your console screen, it means the script is good to run as Node.js script. Alright? So that is our basic Node.js script.
How To Call Node.js Script From Python?
Now, we want to call this hello.js script file from a Python file. But how do we go about doing that? Well, here is the process we are going to follow:
What happens under the hood
- We know that our application program – be it Python code or Node.js code runs on top of our Operating System (OS). Right?
- But how does an OS run both these programs simultaneously? Well the answer is that they both run as two separate processes in an OS.
- But why two separate processes, you ask? It is because it is the job of an OS to make sure both can run paralelly. And it is also the OS’s job to keep these programs separate. Meaning Python program will not be able to access, read or modify the memory of Node.js program and vice versa.
- So that is why OS runs them in a sandbox application process where one process is not aware or have access to other process’s resources.
- But then how can we now call Node.js script from Python script then? Well for that, we need to take help of the OS itself.
- We need to let the OS know that we intend to call Node.js script from our Python script.
- So to do that, we will make use of a special Python library module called “subprocess”.
- But how do we install this module? Well that’s the best thing about this. It is not an external module that needs to be installed separately. It already comes as part of the Python’s built-in packages.
So with this understanding, we will now write a simple Python script that calls the hello.js script written above using subprocess. Sounds good? Great! Let us check it out then!
A simple script to call Node.js script From Python
So create a file called hello_js.py and add the following code to it:
from subprocess import check_output p = check_output(['node', '~/hello.js']) print (p)
Let me explain you briefly on the above Python code:
Line 1: Here we are importing a function from subprocess called check_output. We will use this function to get the console output coming in from our hello.js file.
Line 2: Here we are calling the imported check_output() and passing in a parameter list to it. This list consists of the console command line arguments we wish to execute. Now if you recall, to call our hello.js file using node, what command we used? So if you noticed, we are passing the same set of commands in this parameter list! Now it all makes sense right?
Line 3: Finally, we print the output we got from our check_output() function.
So with this, I will end this article now. But if you have any questions around it, do let me know in the comment section below. I will be more than happy to help!
So until next time, take care!