简体   繁体   English

如何从node.js的python-shell中的python代码获取返回值?

[英]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. 我想在tmp.js中获取tmp.py的返回值。 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 修改您的tmp.py ,以便调用所需的函数

     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())') 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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