[英]nodejs PythonShell, how to export variables in run function
I want to export variable named completed so that I can use this out of PythonShell.run function.我想导出名为 completed 的变量,以便我可以在 PythonShell.run 函数之外使用它。 Is there any suggestion?
有什么建议吗? here's my code.
这是我的代码。
python code蟒蛇代码
#test.py
import sys
def hello(a,b):
print("hello and that's your sum:", a + b)
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
hello(a, b)
javascript code javascript代码
let {PythonShell} = require('python-shell')
let options = {
mode: 'text',
pythonOptions: ['-u'], // get print results in real-time
args: [1414, 2323]
};
PythonShell.run('./photos/filterPhoto/test.py', options, function (err, results) {
if (err) throw err;
const completed = results;
///results: ["hello and that's your sum: 3737"]
});
console.log(completed)
Error : ReferenceError: completed is not defined
Try using JSPyBridge/pythonia to call the Python script:尝试使用JSPyBridge/pythonia调用 Python 脚本:
Through ES6 imports:通过 ES6 导入:
import { python } from 'pythonia'
const test = await python("./test.py")
await test.hello(2, 3)
python.exit()
Or through CommonJS imports:或者通过 CommonJS 导入:
const { python } = require('pythonia')
async function main() {
const test = await python("./test.py")
await test.hello(2, 3)
}
main().then(() => python.exit())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.