簡體   English   中英

使用 pyinstaller 創建 exe 后沒有名為“scipy”的模塊

[英]No module named 'scipy' after creating exe with pyinstaller

所以我有一個腳本,它使用 PIL、numpy 和 scipy 打印圖像的主色:

from PIL import Image
import numpy as np
import scipy.cluster

def dominant_color(image):
    NUM_CLUSTERS = 5

    image = image.resize((150, 150))      # optional, to reduce time
    ar = np.asarray(image)
    shape = ar.shape
    ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)

    codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)

    vecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes
    counts, bins = np.histogram(vecs, len(codes))    # count occurrences

    index_max = np.argmax(counts)                    # find most frequent
    color = tuple([int(code) for code in codes[index_max]])
    return color

image = Image.open("image.jpg")
print(dominant_color(image))

我使用命令pyinstaller --onefile --hidden-import=scipy test.py使用 pyinstaller 創建了一個 exe 但即使在運行 exe 時使用隱藏導入,我也會得到ModuleNotFoundError: No module named 'scipy'我也嘗試添加scipy.cluster作為隱藏導入,但我仍然得到同樣的錯誤。 我在這里錯過了隱藏的導入嗎?

我嘗試了您的代碼並使用它生成了 exe,使用以下命令

pyinstaller --onefile --hidden-import=pkg_resources.py2_warn test.py

我沒有收到任何錯誤。

我的建議是首先嘗試使用上述命令。 如果這不起作用,那么您可能需要檢查您的環境變量,以及系統中是否存在安裝多個 python 的可能性。

我是如何解決的:

使用以下代碼在目錄中創建一個文件:

from PyInstaller.utils.hooks import collect_submodules
from PyInstaller.utils.hooks import collect_data_files
hiddenimports = collect_submodules('scipy')

datas = collect_data_files('scipy')

然后使用--additional-hooks-dir=.newFileName.py運行命令

暫無
暫無

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

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