簡體   English   中英

當我在 nodeJS 中將代碼作為 python shell 運行時,如何永久安裝 python 包?

[英]How can I install python packages permanently when I am running my code as a python shell in nodeJS?

我正在嘗試安裝 cv2 和 nmpy 等模塊,找到了一些解決方案,但使用它們時,每次使用 shell 時都會進行安裝,這會增加響應時間。

這是我用來調用 python function 的 nodeJS 中的 function

`app.post("/img", (req, res) => {
    callNumber(req,res);
});
function callNumber(req,res){
    var spawn= require('child_process').spawn;
    var process = spawn('python',    
    ['./saved_model_exec.py',req.body.imgURL]);
    process.stdout.on('data',function(data){
    res.send(data.toString());
})
}`

這是我試圖運行的 python 文件

`import tensorflow as tf
 import cv2
 import numpy as np
 import sys
 model = tf.keras.models.load_model('mnist.h5')
 img = cv2.imread('test.png', 0)
 img_resized = img.resize(28, 28)
 img_resized = np.array(img)
 img_reshaped = img_resized.reshape(1, 28, 28, 1)
 img_reshaped = img_reshaped/255.0
 res = model.predict([img_reshaped])[0]
 digit, accuracy = np.argmax(res), max(res)
 print(str(digit))`

在運行 nodejs 代碼之前,使用pip install安裝 python 包。 如果您的系統上還沒有 pip,請參閱此處了解如何安裝它。

例如,使用 pip 安裝 numpy: pip install numpy

如果您對 python 和 nodejs 使用相同的實例:

  • 在 python 文件旁邊添加一個 requirementsx.txt 文件
  • 在啟動 nodejs 進程之前使用Procfile安裝依賴項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM