简体   繁体   English

从nodejs应用程序调用时Python脚本不执行pickle.load

[英]Python script not executing pickle.load when called from nodejs app

I have an nodejs app which calls python script from a controller.我有一个 nodejs 应用程序,它从 controller 调用 python 脚本。 Scripts run fine when I run from terminal directly and works fine as well when called from node app but it only stucks on this specific line model = pickle.load(open("model.pkl", "rb")) .当我直接从终端运行时,脚本运行良好,从节点应用程序调用时也运行良好,但它只停留在这一特定行model = pickle.load(open("model.pkl", "rb"))上。 Any help would be appreciated.任何帮助,将不胜感激。

predict.py预测.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()

results.js结果.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());

})

Fixed by changing python version in spawn:通过在 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.

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