How to call Python function from Node.js

I have an Express Node.js application, but I also have a machine learning algorithm for use in Python. Is there a way to call Python functions from my Node.js application to take advantage of the power of machine learning libraries?

+121
python express
May 3 '14 at
source share
6 answers

The easiest way I know is to use the child_process package that comes with the node.

Then you can do something like:

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

Then all you have to do is make sure you import sys into your python script, and then you can access arg1 with sys.argv[1] , arg2 with sys.argv[2] and so on .

To send data back to the node, just do the following in a python script:

 print(dataToSendBack) sys.stdout.flush() 

And then the node can listen to the data using:

 pythonProcess.stdout.on('data', (data) => { // Do something with the data returned from python script }); 

Since this allows multiple arguments to be passed to the script using spawn, you can restructure the python script so that one of the arguments decides which function to call, and the other argument is passed to that function, etc.

Hope this was clear. Let me know if something needs to be clarified.

+162
May 04 '14 at 4:47
source share
β€” -

An example for people who have experience with Python and want to integrate their machine learning model into a Node.js application:

It uses the main child_process module:

 const express = require('express') const app = express() app.get('/', (req, res) => { const { spawn } = require('child_process'); const pyProg = spawn('python', ['./../pypy.py']); pyProg.stdout.on('data', function(data) { console.log(data.toString()); res.write(data); res.end('end'); }); }) app.listen(4000, () => console.log('Application listening on port 4000!')) 

This does not require the sys module in your Python script.

The following is a more modular way of completing a task using Promise :

 const express = require('express') const app = express() let runPy = new Promise(function(success, nosuccess) { const { spawn } = require('child_process'); const pyprog = spawn('python', ['./../pypy.py']); pyprog.stdout.on('data', function(data) { success(data); }); pyprog.stderr.on('data', (data) => { nosuccess(data); }); }); app.get('/', (req, res) => { res.write('welcome\n'); runPy.then(function(fromRunpy) { console.log(fromRunpy.toString()); res.end(fromRunpy); }); }) app.listen(4000, () => console.log('Application listening on port 4000!')) 
+61
Dec 05 '17 at 14:25
source share

extrabacon python-shell extrabacon is an easy way to run Python scripts from Node.js with basic but effective interprocess communication and better error handling.

Installation: npm install python-shell .

Running a simple Python script:

 var PythonShell = require('python-shell'); PythonShell.run('my_script.py', function (err) { if (err) throw err; console.log('finished'); }); 

Running a Python script with arguments and parameters:

 var PythonShell = require('python-shell'); var options = { mode: 'text', pythonPath: 'path/to/python', pythonOptions: ['-u'], scriptPath: 'path/to/my/scripts', args: ['value1', 'value2', 'value3'] }; PythonShell.run('my_script.py', options, function (err, results) { if (err) throw err; // Results is an array consisting of messages collected during execution console.log('results: %j', results); }); 

For full documentation and source code, check out https://github.com/extrabacon/python-shell

+20
Dec 18 '17 at 10:42 on
source share

The child process module for Node.js provides functionality for running scripts or commands in languages ​​other than JavaScript (such as Python). We can implement machine learning algorithms, deep learning algorithms, and many of the features provided through the Python library in the Node.js application. A child module of the process allows us to run a Python script in a Node.js application and transfer / output data to / from a Python script.

child_process.spawn (): This method helps us spawn a child process asynchronously.

Let's create a simple Python script that will take two command line arguments as a first and last name, and then display them. Later, run this script well from the Node.js application and display the output in a browser window.

Python script:

 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:

 // Import Express.js JavaScript module into the application // 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 // the 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 // Eg: 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-side script code, run the code from its source folder using the following command:

 node start.js 
+12
May 31 '18 at 14:52
source share

I am on node 10 and the child process is 1.0.2 . Data from python is a byte array and needs to be converted. Another quick example of executing an http request in python.

knot

 const process = spawn("python", ["services/request.py", "https://www.google.com"]) return new Promise((resolve, reject) =>{ process.stdout.on("data", data =>{ resolve(data.toString()); // <------------ by default converts to utf-8 }) process.stderr.on("data", reject) }) 

request.py

 import urllib.request import sys def karl_morrison_is_a_pedant(): response = urllib.request.urlopen(sys.argv[1]) html = response.read() print(html) sys.stdout.flush() karl_morrison_is_a_pedant() 

ps is not a far-fetched example, since the node http module does not load a few requests that I need to make

+1
Jul 31 '18 at 16:50
source share

You can take your python, transfer it, and then name it as if it were JavaScript. I did it successfully for screeps and even made it work in the browser in my own way .

0
May 6 '19 at 10:41
source share



All Articles