Skip to content
Advertisement

How to call python script from NodeJs

I need to call this python script in NodeJs.

Read.py

JavaScript

I used python-shell, here is the NodeJs code for that

Test.js

JavaScript

But when I ran this code I didn’t see anything in Node side. I think problem occurs when python script comes to this level.

JavaScript

Because I just ran with while loop which has only print statement then it works. After that I tried another way to achieve this. But I got same problem which I have with above.Here is another method

AltTest.js

JavaScript

Any help would be appreciated

Advertisement

Answer

There are multiple ways of doing this.

  • first way is by doing npm install python-shell

and here’s the code

JavaScript

you can send a message to python shell using pyshell.send('hello');

you can find the API reference here- https://github.com/extrabacon/python-shell

a few more references – https://www.npmjs.com/package/python

if you want to use service-oriented architecture – http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

Advertisement