简体   繁体   中英

How to get return value from python codes in python-shell of node.js?

I want to get return value of tmp.py in tmp.js. How can I solve this?

def function():
    return 'tmp'
const {PythonShell} = require('python-shell');
PythonShell.run('tmp.py')

A file does not have a 'return' value. A function however, does.

Either:

  • modify your tmp.py such that the desired function gets called

     def function() return 'tmp' if __name__ == '__main__': var = function() print(var) 
  • or directly run the code in the shell

      const {PythonShell} = require('python-shell'); PythonShell.run('import tmp') PythonShell.run('print(tmp.function())') 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM