python-shell v5.0.0:带回调的run方法无响应,带then的却正常?
问题原因与解决方案
你遇到的问题核心在于python-shell 5.0.0版本已完全移除了回调风格的API:从该版本开始,PythonShell.run方法仅返回Promise对象,不再支持传入第三个回调参数,因此你编写的回调函数永远不会被触发。
验证与解决办法
继续使用Promise风格(推荐)
若保留5.x版本,可继续使用Promise链式调用,或改用更简洁的async/await写法:const {PythonShell} = require("python-shell"); async function runPythonScript() { try { const result = await PythonShell.run("hello.py", null); console.log(result[0]); } catch (err) { console.log("err = ", err); } } runPythonScript();降级到支持回调的版本
若坚持使用回调风格,可将python-shell降级到4.x版本(如4.0.1),执行命令:npm install python-shell@4.0.1 --save降级后你原本的回调写法就能正常执行。
内容的提问来源于stack exchange,提问作者Ronald Becerra
相关产品推荐
相关产品推荐

