[英]Python script not executing pickle.load when called from nodejs app
我有一个 nodejs 应用程序,它从 controller 调用 python 脚本。 当我直接从终端运行时,脚本运行良好,从节点应用程序调用时也运行良好,但它只停留在这一特定行model = pickle.load(open("model.pkl", "rb"))
上。 任何帮助,将不胜感激。
预测.py
import sys
import json
import pickle
import os
def get_size(weight, height, age):
model = pickle.load(open("model.pkl", "rb"))
return model.predict([[height,weight,age,bmi]])[0]
calculated_size = get_size(69,167,26)
print(calculated_size)
sys.stdout.flush()
结果.js
const scriptPath = path.dirname(require.main.filename) + '/python-scripts/predict.py';
const pythonProcess = spawn('python', [ scriptPath, 'user_inputs', JSON.stringify(results) ], {cwd: path.dirname(require.main.filename)+'/python-scripts'});
pythonProcess.stdout.on('data', async (data) => {
console.log(data.toString());
})
通过在 spawn 中更改 python 版本来修复:
const pythonProcess = spawn('python3', [ scriptPath, 'user_inputs', JSON.stringify(results) ], {cwd: path.dirname(require.main.filename)+'/python-scripts'});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.