How to call python script from NodeJs

后端 未结 5 950
轻奢々
轻奢々 2020-12-01 07:06

I need to call this python script in NodeJs.

Read.py

#!/usr/bin/env python
# -*- coding: utf8 -*-

import RPi.GPIO as GPIO
import MF         


        
5条回答
  •  Happy的楠姐
    2020-12-01 07:33

    There are multiple ways of doing this.

    • first way is by doing npm install python-shell

    and here's the code

    var PythonShell = require('python-shell');
    //you can use error handling to see if there are any errors
    PythonShell.run('my_script.py', options, function (err, results) { 
    //your code
    

    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

    • second way - another package you can refer to is node python , you have to do npm install node-python

    • third way - you can refer to this question where you can find an example of using a child process- How to invoke external scripts/programs from node.js

    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/

提交回复
热议问题