繁体   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