简体   繁体   中英

No module named 'scipy' after creating exe with pyinstaller

So I have a script that prints the dominant colour of an image using PIL, numpy and 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))

I create an exe using pyinstaller using the command pyinstaller --onefile --hidden-import=scipy test.py But even with hidden import when I run the exe I get ModuleNotFoundError: No module named 'scipy' I have also tried adding scipy.cluster as a hidden import but I still get the same error. Am I missing a hidden import here?

I tried your code and generated exe with it, using below command

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

I am getting no errors.

My suggestion is to first try out with above command. If that doesn't work, then you may need to check your environment variables, and whether there is any possibility of multiple python installed in the system.

How I fixed it:

create a file in the directory with the following code:

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

datas = collect_data_files('scipy')

Then run the command with --additional-hooks-dir=.newFileName.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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