Run Python script from Node.js using child process spawn() method Last Updated : 19 Feb, 2019 Comments Improve Suggest changes Like Article Like Report Node.js is one of the most adopted web development technologies but it lacks support for machine learning, deep learning and artificial intelligence libraries. Luckily, Python supports all these and many more other features. Django Framework for Python can utilize this functionality of Python and can provide support for building new age web application using machine learning and Artificial Intelligence. For those developers who are not familiar with Django Framework but use Node JS framework can also benefit from Python using child process module for Node JS. Child Process module for Node JS provides functionality to run scripts or commands in languages other than JavaScript too (like Python). We can implement machine learning algorithms, deep learning algorithms and many features provided via Python library into Node JS application. Child Process allows us to run Python script in Node JS application and stream in/out data into/from Python script. child_process.spawn(): This method helps us to spawn child process asynchronously. Let's create a simple Python script that will take two command line arguments as a first name and last name then display them. Later we'll run that script from Node JS application and display output in the browser window. Python script : Python3 import sys # Takes first name and last name via command # line arguments and then display them print("Output from Python") print("First name: " + sys.argv[1]) print("Last name: " + sys.argv[2]) # save the script as hello.py Node JS server code : Java // import express JS module into app // and creates its variable. var express = require('express'); var app = express(); // Creates a server which runs on port 3000 and // can be accessed through localhost:3000 app.listen(3000, function() { console.log('server running on port 3000'); } ) // Function callName() is executed whenever // url is of the form localhost:3000/name app.get('/name', callName); function callName(req, res) { // Use child_process.spawn method from // child_process module and assign it // to variable spawn var spawn = require("child_process").spawn; // Parameters passed in spawn - // 1. type_of_script // 2. list containing Path of the script // and arguments for the script // E.g : http://localhost:3000/name?firstname=Mike&lastname=Will // so, first name = Mike and last name = Will var process = spawn('python',["./hello.py", req.query.firstname, req.query.lastname] ); // Takes stdout data from script which executed // with arguments and send this data to res object process.stdout.on('data', function(data) { res.send(data.toString()); } ) } // save code as start.js After saving the Python script and server script code, run the code from its source folder by following command : node start.js Access the application through link : localhost:3000/name?firstname="Enter first name"&lastname="Enter last name" For e g. : localhost:3000/name?firstname=Ram&lastname=Sharma Output : Applications : This method can be used in alternative to REST-APIs. This method can help our web application take benefits from other languages special features which are currently not available in javascript Machine learning modules can be implemented in Python and then utilize them in web app using this method. Reference : https://nodejs.org/api/child_process.html Comment More infoAdvertise with us Next Article Run Python script from Node.js using child process spawn() method N neerajnegi174 Follow Improve Article Tags : Python JavaScript Web Technologies JavaScript-Questions Practice Tags : python Similar Reads Run Python Script using PythonShell from Node.js Nowadays Node.js is the most attractive technology in the field of backend development for developers around the globe. And if someone wishes to use something like Web Scraping using python modules or run some python scripts having some machine learning algorithms, then one need to know how to integ 3 min read Node.js process.setuid() Method The process.setuid() method is an inbuilt application programming interface of the process module which is used to set the user identity of the Node.js process. Syntax:Â process.setuid(id) Parameters: This method accepts single parameter as mentioned above and described below:Â Â id: It is a required 2 min read Understanding âforkâ and âspawnâ in Python Multiprocessing Pythonâs multiprocessing library provides a powerful way to leverage multiple processor cores for concurrent execution, enhancing the performance of computationally intensive tasks. One of the intriguing aspects of multiprocessing is the ability to initiate new processes using various start methods. 6 min read Node.js process.seteuid() Method The process.seteuid() method is an inbuilt application programming interface of the process module which is used to set the effective user identity of the Node.js process. Syntax:Â process.seteuid( id ) Parameters: This method accepts single parameter as mentioned above and described below:Â Â id: It 2 min read Node.js process.setgid() Method The process.setgid() method is an inbuilt application programming interface of the process module which is used to set the group identity of the Node.js process.Syntax:Â process.setgid(id) Parameters: This method accepts single parameter as mentioned above and described below:Â id: It is a required 2 min read Node.js process.send() Method The process.send() method is an inbuilt application programming interface of the process module which is used by the child process to communicate with the parent process. This method does not work for the root process because it does not have any parent process. Syntax: process.send(message, [sendHa 2 min read How to Get the Path of Current Script using Node.js ? In Node JS, getting the path of the current script is useful for file operations, logging, and configuration. It allows you to access related files or directories reliably, regardless of where your Node.js process was started.ApproachTo get the path of the present script in node.js we will be using 2 min read Node.js process.getuid() Method The process.getuid() method is an inbuilt application programming interface of the process module which is used to get the numerical user identity of the Node.js process. Syntax: process.getuid() Parameters: This method does not accept any parameters. Return Value: This method returns an integer val 1 min read Node.js process.exit() Method The process.exit() method is used to end the process which is running at the same time with an exit code in NodeJS. Syntax: process.exit( code ) Parameter: Â This function accepts single parameter as mentioned above and described below: Code: It can be either 0 or 1. 0 means end the process without a 2 min read Node.js | script.runInThisContext() Method The script.runInThisContext() method runs the compiled code present inside the vm.Script within the context of the current global object. Moreover, running code has no access to local scope, but it has access to the current global object. Syntax: script.runInThisContext( options ) Parameters: This m 2 min read Like