简体   繁体   English

[Pyinstaller]:工作代码在转为 exe 时会产生 ModuleNotFoundError

[英][Pyinstaller]: Working code produces ModuleNotFoundError when turned to exe

Working with Pyinstaller 3.5 and python 3.6 (installed with Anaconda).使用 Pyinstaller 3.5 和 python 3.6(与 Anaconda 一起安装)。

I have a python code that explicitly imports numpy and works fine when launched with Spyder or the Anaconda prompt.我有一个 python 代码,它明确导入 numpy 并且在使用 Spyder 或 Anaconda 提示符启动时工作正常。

Using pyinstaller to produce an exe in the Anaconda prompt, it generates fine without any error.在 Anaconda 提示符下使用 pyinstaller 生成一个 exe,它生成正常,没有任何错误。

However, when launching the exe (still from the Anaconda prompt), it produces the following error: ModuleNotFoundError: No module named 'numpy'但是,在启动 exe 时(仍然来自 Anaconda 提示符),它会产生以下错误: ModuleNotFoundError: No module named 'numpy'

I checked that numpy was correctly installed with Anaconda.我检查了 numpy 是否与 Anaconda 正确安装。 I also installed it again with: pip install numpy and regenerated the exe without success (more of a despair move).我还再次安装了它: pip install numpy exe 没有成功(更多的是绝望的举动)。

I also tried -hidden-import=numpy but it did not work.我也试过-hidden-import=numpy但它没有用。 Still the same error.还是同样的错误。

Do you guys have any idea why this does not work?你们知道为什么这不起作用吗?

I made a shorter piece of code to test the issue:我做了一段较短的代码来测试这个问题:

# import all the libraries required
import os, re, time, csv, math

import numpy as np
import matplotlib
import matplotlib.figure
import matplotlib.backends.backend_tkagg
import matplotlib.patches
import matplotlib.gridspec
import itertools

from tkinter import filedialog
from tkinter import messagebox

import tkinter
import tkinter.constants as const
from tkinter import ttk

import xlrd # Read Xls files

# Compute the used colors
colors = {  "white":        [ 255,255,255],
            "lightgreen":   [ 192,255,192],
            "red":          [ 255, 64, 64],
            "grey":         [ 224,224,224],
            "darkgrey":     [  64, 64, 64],
            "green":        [   0,255,  0],
            "blue":         [ 150,150,255],
            "orange":       [ 255,190,  0],
         }
color0 = colors["red"]
color1 = colors["lightgreen"]
colorMap = np.array([color0, color1], dtype = np.uint8)

The solution to the problem was to run Anaconda as Admin to install pyinstaller through Anaconda.该问题的解决方案是以管理员身份运行 Anaconda 以通过 Anaconda 安装 pyinstaller。 In the shortcut menu, right click on Anaconda and run as Admin.在快捷菜单中,右键单击 Anaconda 并以管理员身份运行。 Then, installing new modules is possible.然后,可以安装新模块。 Install pyinstaller.安装 pyinstaller。 Run pyinstaller in Anaconda prompt.在 Anaconda 提示符下运行 pyinstaller。

The ModuleNotFoundError disappeared and the exe runs fine when launched from Anaconda prompt.从 Anaconda 提示符启动时,ModuleNotFoundError 消失了,exe 运行良好。

Still does not work when launched from finder though it's another problem.从 finder 启动时仍然无法正常工作,尽管这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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