如何在使用matplotlib时使用pyinstaller

8

我有一个脚本,我在前面添加了GUI并希望将其分发给其他DnD DMs使用,以便他们可以将网格叠加到图像上。唯一的问题是,每次我尝试使用Pyinstaller打包Python脚本时,它都会抛出两个不同的错误。如果我运行pyinstaller --hidden-import matplotlib myscript.py,它将返回:

    NameError: name 'defaultParams' is not defined
    [7532] Failed to execute script ImageGridder

于是我决定再次运行该命令,并加上 --onefile 选项。当我这样做时,它返回如下结果:

   RuntimeError: Could not find the matplotlib data files
   [18884] Failed to execute script ImageGridder

在这两个例子中,打包过程完成了,所有文件似乎都生成正确。只是当我运行生成的 .exe 文件时,它会崩溃。我知道文件本身可以正常运行,因为我已经在我的台式机和笔记本电脑上成功地运行并工作。我已经花费了相当长的时间搜寻任何可帮助的东西,但似乎没有什么真正有用。脚本本身

from PIL import Image
import tkinter as tk
from tkinter import filedialog
import matplotlib.pyplot as PLT
import matplotlib.ticker as plticker

class gridder(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.initialize()

    def initialize(self):        
        self.iWidth = tk.StringVar()
        self.iHeight = tk.StringVar()
        self.imgSelect = tk.StringVar()
        self.squareLength= tk.IntVar()
        self.ratioX=tk.IntVar()
        self.ratioY=tk.IntVar()
        self.checkSquare = tk.IntVar()
        self.colorLine= tk.StringVar()
        
        self.colorLine.set('k')
        self.checkSquare.set(0)
        self.ratioX.set(10)
        self.ratioY.set(10)
        self.squareLength.set(120)
        

        # row 1
        labelDisclaim = tk.Label(self, text='Currently only works with jpegs')
        labelDisclaim.grid(column=2, row=1)

        # row 2
        buttonOpen = tk.Button(self, text="Select an Image", command=self.openExplorer)
        buttonOpen.grid(column=1, row=2)

        labelSelected= tk.Label(self, text="Selected Image: ")
        labelSelected.grid(column=2,row=2)

        labelImgName = tk.Label(self, textvariable=self.imgSelect)
        labelImgName.grid(column=3,row=2)
        

        # row 3
        labelStaticImg= tk.Label(self, text="Width of image, in pixels: ")
        labelStaticImg.grid(column=1,row=3)

        labelImgWidth = tk.Label(self, textvariable=self.iWidth, anchor='w')
        labelImgWidth.grid(column=2,row=3)

        labelStaticHeight= tk.Label(self, text="Height of image, in pixels: ")
        labelStaticHeight.grid(column=3,row=3)

        labelImgHeight = tk.Label(self, textvariable=self.iHeight, anchor='w')
        labelImgHeight.grid(column=4,row=3)

        # row 4
        labelRatioX = tk.Label(self, text="Enter the Ratio along the X axis, default is 10: ")
        labelRatioX.grid(column=1,row=4)

        entryRatioX = tk.Entry(self, textvariable=self.ratioX)
        entryRatioX.grid(column=2,row=4)

        labelRatioY =tk.Label(self, text="Enter the Ratio along the Y axis, default is 10: ")
        labelRatioY.grid(column=3,row=4)

        entryRatioY = tk.Entry(self, textvariable=self.ratioY)
        entryRatioY.grid(column=4,row=4)

        # row 5
        labelSquare = tk.Label(self, text="For strict squares, in the sense of a battle map, check this ->")
        labelSquare.grid(column=1,row=5)

        checkboxSquare = tk.Checkbutton(self, variable=self.checkSquare, text="If checked, it will ignore the ratio and apply squares that are specified by the entry, (default 120x120) ->",wraplength=150)
        checkboxSquare.grid(column=2,row=5)

        labelSquareLength = tk.Label(self, text="Side length of Square: ")
        labelSquareLength.grid(column=3,row=5)

        entrySquareLength = tk.Entry(self, textvariable=self.squareLength)
        entrySquareLength.grid(column=4,row=5)

        
        # row 6
        labelColor= tk.Label(self, text="Enter a color for the grid, valid choices black=k, blue=b, green=g, red=r, white=w, brown=brown, yellow=yellow, cyan=c. Default is black: ",wraplength=250)
        labelColor.grid(column=1,row=6)

        entryColor = tk.Entry(self, textvariable=self.colorLine)
        entryColor.grid(column=2,row=6)
        
        execButton = tk.Button(self, text="Gridify", command=self.gridify)
        execButton.grid(column=4,row=6)
        
        # row 9
        button = tk.Button(self,text="Exit",command=self.closeProgram)
        button.grid(column=2,row=9)

        # row 10
        labelSig = tk.Label(self, text='By Johnathan Keith, 2020. Ver 1.0. This is free-to-use, and will always be. This was willingly distributed to the public.',wraplength=350)
        labelSig.grid(column=2,row=10)

        labelDisclaimer = tk.Label(self, text="This program does NOT generate pop up windows for bad data entries. If the image does not generate into the folder the script is in, you did something wrong.",wraplength=200)
        labelDisclaimer.grid(column=4,row=10)

    def openFile(self, imagefilename):
        Img = Image.open(imagefilename)
        height, width = Img.size
        self.iHeight.set(height)
        self.iWidth.set(width)

    def gridify(self):
        ratioX=0
        ratioY=0
        sidelengthy=0
        sidelengthx=0
        if self.checkSquare.get():
            ratioX=int(self.squareLength.get())
            ratioY=int(self.squareLength.get())
            sidelengthx=ratioX
            sidelengthy=ratioY
        else:
            ratioX=int(self.ratioX.get())
            ratioY=int(self.ratioY.get())
            sidelengthy=int(self.iWidth.get())/ratioY
            sidelengthx=int(self.iHeight.get())/ratioX
        image=Image.open(self.imgSelect.get())
        my_dpi=300.

        #set the figure up
        fig=PLT.figure(figsize=(float(image.size[0])/my_dpi,float(image.size[1])/my_dpi),dpi=my_dpi)
        ax=fig.add_subplot(111)

        #remove whitespace
        fig.subplots_adjust(left=0,right=1,bottom=0,top=1)

        #set gridding interval
        locx = plticker.MultipleLocator(base=sidelengthx)
        locy = plticker.MultipleLocator(base=sidelengthy)
        ax.xaxis.set_major_locator(locx)
        ax.yaxis.set_major_locator(locy)

        #add the grid
        ax.grid(which='major', axis='both', linestyle='-',color=self.colorLine.get())

        ax.imshow(image)

        token=self.imgSelect.get().split('/')
        saveName= "gridded_"+token[-1]
        
        # Save the figure
        fig.savefig(saveName,dpi=my_dpi)

    def closeProgram(self):
        self.destroy()
        exit()

    def dataEntry(self):
        if type(int) == type(int(bHeight)):
            self.bHeight = int(entryHeight.get())
        else:
            return
        
    def openExplorer(self):
        filename= filedialog.askopenfilename(initialdir="/", title="Select an Image", filetypes=(("jpeg files", "*.jpg"),("all files", "*.*")))
        if filename:
           self.imgSelect.set(filename)
           self.openFile(filename)
           

if __name__ == "__main__":
    app = gridder()
    app.title('Image Gridder')
    app.mainloop()

我正在使用Python 3.8、Matplotlib 3.3.0、Tkinter和PIL 6.2.2以及Pyinstaller 3.6。

在使用--onefile命令时,警告文件的内容如下:


This file lists modules PyInstaller was not able to find. This does not
necessarily mean this module is required for running you program. Python and
Python 3rd-party packages include a lot of conditional or optional modules. For
example the module 'ntpath' only exists on Windows, whereas the module
'posixpath' only exists on Posix systems.

Types if import:
* top-level: imported at the top-level - look at these first
* conditional: imported within an if-statement
* delayed: imported from within a function
* optional: imported within a try-except-statement

IMPORTANT: Do NOT post this list to the issue-tracker. Use it as a basis for
           yourself tracking down the missing module. Thanks!

missing module named _posixsubprocess - imported by subprocess (optional), multiprocessing.util (delayed)
missing module named 'org.python' - imported by copy (optional), xml.sax (delayed, conditional), setuptools.sandbox (conditional)
missing module named _frozen_importlib_external - imported by importlib._bootstrap (delayed), importlib (optional), importlib.abc (optional), zipimport (top-level)
excluded module named _frozen_importlib - imported by importlib (optional), importlib.abc (optional), zipimport (top-level), PyInstaller.loader.pyimod02_archive (delayed, conditional)
missing module named urllib.pathname2url - imported by urllib (conditional), PyInstaller.lib.modulegraph._compat (conditional)
missing module named _posixshmem - imported by multiprocessing.resource_tracker (conditional), multiprocessing.shared_memory (conditional)
missing module named multiprocessing.set_start_method - imported by multiprocessing (top-level), multiprocessing.spawn (top-level)
missing module named multiprocessing.get_start_method - imported by multiprocessing (top-level), multiprocessing.spawn (top-level)
missing module named _scproxy - imported by urllib.request (conditional)
missing module named termios - imported by tty (top-level), getpass (optional)
missing module named resource - imported by posix (top-level), test.support (optional)
missing module named 'java.lang' - imported by platform (delayed, optional), xml.sax._exceptions (conditional)
missing module named vms_lib - imported by platform (delayed, conditional, optional)
missing module named java - imported by platform (delayed)
missing module named _winreg - imported by platform (delayed, optional), numpy.distutils.cpuinfo (delayed, conditional, optional), pkg_resources._vendor.appdirs (delayed, conditional)
missing module named multiprocessing.get_context - imported by multiprocessing (top-level), multiprocessing.pool (top-level), multiprocessing.managers (top-level), multiprocessing.sharedctypes (top-level)
missing module named multiprocessing.TimeoutError - imported by multiprocessing (top-level), multiprocessing.pool (top-level)
missing module named multiprocessing.BufferTooShort - imported by multiprocessing (top-level), multiprocessing.connection (top-level)
missing module named multiprocessing.AuthenticationError - imported by multiprocessing (top-level), multiprocessing.connection (top-level)
missing module named asyncio.DefaultEventLoopPolicy - imported by asyncio (delayed, conditional), asyncio.events (delayed, conditional)
missing module named readline - imported by cmd (delayed, conditional, optional), code (delayed, conditional, optional), pdb (delayed, optional)
missing module named org - imported by pickle (optional)
missing module named grp - imported by shutil (optional), tarfile (optional), pathlib (delayed), distutils.archive_util (optional)
missing module named pwd - imported by posixpath (delayed, conditional), shutil (optional), tarfile (optional), pathlib (delayed, conditional, optional), http.server (delayed, optional), webbrowser (delayed), netrc (delayed, conditional), getpass (delayed), distutils.util (delayed, conditional, optional), distutils.archive_util (optional)
missing module named posix - imported by os (conditional, optional), shutil (conditional)
missing module named 'multiprocessing.forking' - imported by C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_multiprocessing.py (optional)
missing module named 'win32com.gen_py' - imported by win32com (conditional, optional), C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_win32comgenpy.py (top-level)
missing module named pyimod03_importers - imported by PyInstaller.loader.pyimod02_archive (delayed, conditional), C:\Users\Owner\AppData\Local\Programs\Python\Python38-32\Lib\site-packages\PyInstaller\loader\rthooks\pyi_rth_pkgres.py (top-level)
missing module named 'pkg_resources.extern.pyparsing' - imported by pkg_resources._vendor.packaging.requirements (top-level), pkg_resources._vendor.packaging.markers (top-level)
missing module named _uuid - imported by uuid (optional)
missing module named __builtin__ - imported by PIL.Image (optional), numpy.core.numerictypes (conditional), numpy.core.numeric (conditional), numpy.lib.function_base (conditional), numpy.lib._iotools (conditional), numpy.ma.core (conditional), numpy.distutils.misc_util (delayed, conditional), numpy (conditional), pyparsing (conditional), pkg_resources._vendor.pyparsing (conditional), setuptools._vendor.pyparsing (conditional)
missing module named ordereddict - imported by pyparsing (optional), pkg_resources._vendor.pyparsing (optional), setuptools._vendor.pyparsing (optional)
missing module named StringIO - imported by PyInstaller.lib.modulegraph._compat (conditional), PyInstaller.lib.modulegraph.zipio (conditional), setuptools._vendor.six (conditional), numpy.lib.utils (delayed, conditional), numpy.lib.format (delayed, conditional), numpy.testing._private.utils (conditional), six (conditional), pkg_resources._vendor.six (conditional)
missing module named 'com.sun' - imported by pkg_resources._vendor.appdirs (delayed, conditional, optional)
missing module named com - imported by pkg_resources._vendor.appdirs (delayed)
missing module named pkg_resources.extern.packaging - imported by pkg_resources.extern (top-level), pkg_resources (top-level)
missing module named pkg_resources.extern.appdirs - imported by pkg_resources.extern (top-level), pkg_resources (top-level)
missing module named 'pkg_resources.extern.six.moves' - imported by pkg_resources (top-level), pkg_resources._vendor.packaging.requirements (top-level)
missing module named pkg_resources.extern.six - imported by pkg_resources.extern (top-level), pkg_resources (top-level), pkg_resources.py31compat (top-level)
missing module named pytest - imported by numpy._pytesttester (delayed), matplotlib (delayed, optional)
missing module named commands - imported by numpy.distutils.cpuinfo (conditional)
missing module named setuptools.extern.packaging - imported by setuptools.extern (top-level), setuptools.dist (top-level), setuptools.command.egg_info (top-level)
missing module named 'setuptools.extern.six' - imported by setuptools (top-level), setuptools.extension (top-level)
missing module named 'setuptools.extern.packaging.version' - imported by setuptools.config (top-level), setuptools.msvc (top-level)
missing module named setuptools.extern.six.moves.filterfalse - imported by setuptools.extern.six.moves (top-level), setuptools.dist (top-level), setuptools.msvc (top-level)
missing module named setuptools.extern.six.moves.filter - imported by setuptools.extern.six.moves (top-level), setuptools.dist (top-level), setuptools.ssl_support (top-level), setuptools.command.py36compat (top-level)
missing module named _manylinux - imported by setuptools.pep425tags (delayed, optional)
missing module named 'setuptools.extern.packaging.utils' - imported by setuptools.wheel (top-level)
missing module named wincertstore - imported by setuptools.ssl_support (delayed, optional)
missing module named 'backports.ssl_match_hostname' - imported by setuptools.ssl_support (optional)
missing module named backports - imported by setuptools.ssl_support (optional)
missing module named 'setuptools._vendor.six.moves' - imported by 'setuptools._vendor.six.moves' (top-level)
missing module named 'setuptools.extern.pyparsing' - imported by setuptools._vendor.packaging.markers (top-level), setuptools._vendor.packaging.requirements (top-level)
missing module named setuptools.extern.six.moves.map - imported by setuptools.extern.six.moves (top-level), setuptools.dist (top-level), setuptools.command.easy_install (top-level), setuptools.sandbox (top-level), setuptools.package_index (top-level), setuptools.ssl_support (top-level), setuptools.command.egg_info (top-level), setuptools.namespaces (top-level)
runtime module named setuptools.extern.six.moves - imported by setuptools.dist (top-level), setuptools.py33compat (top-level), configparser (top-level), setuptools.command.easy_install (top-level), setuptools.sandbox (top-level), setuptools.command.setopt (top-level), setuptools.package_index (top-level), setuptools.ssl_support (top-level), setuptools.command.egg_info (top-level), setuptools.command.py36compat (top-level), setuptools.namespaces (top-level), setuptools.msvc (top-level), 'setuptools._vendor.six.moves' (top-level)
missing module named setuptools.extern.six - imported by setuptools.extern (top-level), setuptools.monkey (top-level), setuptools.dist (top-level), setuptools.extern.six.moves (top-level), setuptools.py33compat (top-level), setuptools.config (top-level), setuptools.command.easy_install (top-level), setuptools.sandbox (top-level), setuptools.py27compat (top-level), setuptools.package_index (top-level), setuptools.wheel (top-level), setuptools.pep425tags (top-level), setuptools.command.egg_info (top-level), setuptools.command.sdist (top-level), setuptools.command.bdist_egg (top-level), setuptools.unicode_utils (top-level), setuptools.command.develop (top-level)
missing module named 'numpy_distutils.cpuinfo' - imported by numpy.f2py.diagnose (delayed, conditional, optional)
missing module named 'numpy_distutils.fcompiler' - imported by numpy.f2py.diagnose (delayed, conditional, optional)
missing module named 'numpy_distutils.command' - imported by numpy.f2py.diagnose (delayed, conditional, optional)
missing module named numpy_distutils - imported by numpy.f2py.diagnose (delayed, optional)
missing module named __svn_version__ - imported by numpy.f2py.__version__ (optional)
missing module named numarray - imported by numpy.distutils.system_info (delayed, conditional, optional)
missing module named Numeric - imported by numpy.distutils.system_info (delayed, conditional, optional)
missing module named ConfigParser - imported by numpy.distutils.system_info (conditional), numpy.distutils.npy_pkg_config (conditional)
missing module named _curses - imported by curses (top-level), curses.has_key (top-level)
missing module named _dummy_threading - imported by dummy_threading (optional)
missing module named 'nose.plugins' - imported by numpy.testing._private.noseclasses (top-level), numpy.testing._private.nosetester (delayed)
missing module named scipy - imported by numpy.testing._private.nosetester (delayed, conditional)
missing module named 'nose.util' - imported by numpy.testing._private.noseclasses (top-level)
missing module named nose - imported by numpy.testing._private.utils (delayed, optional), numpy.testing._private.decorators (delayed), numpy.testing._private.noseclasses (top-level)
missing module named dummy_thread - imported by numpy.core.arrayprint (conditional, optional)
missing module named thread - imported by numpy.core.arrayprint (conditional, optional), PyInstaller.loader.pyimod02_archive (conditional)
missing module named cPickle - imported by numpy.core.numeric (conditional)
missing module named cStringIO - imported by cPickle (top-level)
missing module named copy_reg - imported by cPickle (top-level), cStringIO (top-level), numpy.core (conditional)
missing module named pickle5 - imported by numpy.core.numeric (conditional, optional)
missing module named numpy.core.number - imported by numpy.core (delayed), numpy.testing._private.utils (delayed)
missing module named numpy.core.signbit - imported by numpy.core (delayed), numpy.testing._private.utils (delayed)
missing module named numpy.core.float64 - imported by numpy.core (delayed), numpy.testing._private.utils (delayed)
missing module named numpy.core.float32 - imported by numpy.core (top-level), numpy.testing._private.utils (top-level)
missing module named numpy.lib.i0 - imported by numpy.lib (top-level), numpy.dual (top-level)
missing module named numpy.core.integer - imported by numpy.core (top-level), numpy.fft.helper (top-level)
missing module named numpy.core.sqrt - imported by numpy.core (top-level), numpy.linalg.linalg (top-level), numpy.fft.fftpack (top-level)
missing module named numpy.core.conjugate - imported by numpy.core (top-level), numpy.fft.fftpack (top-level)
missing module named numpy.core.divide - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.object_ - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.intp - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.geterrobj - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.add - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.complexfloating - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.inexact - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.cdouble - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.csingle - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.double - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named numpy.core.single - imported by numpy.core (top-level), numpy.linalg.linalg (top-level)
missing module named future_builtins - imported by numpy.lib.npyio (conditional)
missing module named urllib2 - imported by numpy.lib._datasource (delayed, conditional)
missing module named urlparse - imported by numpy.lib._datasource (delayed, conditional)
missing module named numpy.recarray - imported by numpy (top-level), numpy.ma.mrecords (top-level)
missing module named numpy.dtype - imported by numpy (top-level), numpy.ma.mrecords (top-level), numpy.ctypeslib (top-level)
missing module named numpy.expand_dims - imported by numpy (top-level), numpy.ma.core (top-level)
missing module named numpy.array - imported by numpy (top-level), numpy.ma.core (top-level), numpy.ma.extras (top-level), numpy.ma.mrecords (top-level), numpy.ctypeslib (top-level)
missing module named numpy.bool_ - imported by numpy (top-level), numpy.ma.core (top-level), numpy.ma.mrecords (top-level)
missing module named numpy.iscomplexobj - imported by numpy (top-level), numpy.ma.core (top-level)
missing module named numpy.amin - imported by numpy (top-level), numpy.ma.core (top-level)
missing module named numpy.amax - imported by numpy (top-level), numpy.ma.core (top-level)
missing module named numpy.ndarray - imported by numpy (top-level), numpy.ma.core (top-level), numpy.ma.extras (top-level), numpy.ma.mrecords (top-level), numpy.ctypeslib (top-level)
missing module named numpy.histogramdd - imported by numpy (delayed), numpy.lib.twodim_base (delayed)
missing module named numpy.eye - imported by numpy (delayed), numpy.core.numeric (delayed)
missing module named six.moves.zip - imported by six.moves (top-level), cycler (top-level)
runtime module named six.moves - imported by cycler (top-level), dateutil.tz.tz (top-level), dateutil.tz._factories (top-level), dateutil.tz.win (top-level), dateutil.rrule (top-level)
missing module named six.moves.range - imported by six.moves (top-level), dateutil.rrule (top-level)
missing module named colorama - imported by tornado.log (optional)
missing module named typing_extensions - imported by tornado.ioloop (conditional), tornado.websocket (conditional)
missing module named fcntl - imported by tornado.platform.posix (top-level)
missing module named dateutil.tz.tzfile - imported by dateutil.tz (top-level), dateutil.zoneinfo (top-level)
missing module named shiboken - imported by matplotlib.backends.qt_compat (delayed, conditional)
missing module named PySide - imported by matplotlib.backends.qt_compat (delayed, conditional)
missing module named PyQt4 - imported by matplotlib.backends.qt_compat (delayed)
missing module named shiboken2 - imported by matplotlib.backends.qt_compat (delayed, conditional)
missing module named PySide2 - imported by PIL.ImageQt (conditional, optional), matplotlib.backends.qt_compat (delayed, conditional)
missing module named sip - imported by matplotlib.backends.qt_compat (delayed, conditional, optional), PyQt5 (top-level)
missing module named matplotlib.axes.Axes - imported by matplotlib.axes (top-level), matplotlib.pyplot (top-level), matplotlib.legend (delayed), matplotlib.projections.geo (top-level), matplotlib.projections.polar (top-level), mpl_toolkits.mplot3d.axes3d (top-level), matplotlib.figure (top-level)
missing module named 'IPython.core' - imported by matplotlib.backend_bases (delayed), matplotlib.pyplot (delayed, conditional, optional)
missing module named IPython - imported by matplotlib.backend_bases (delayed), matplotlib.pyplot (delayed, conditional, optional)
missing module named matplotlib.tri.Triangulation - imported by matplotlib.tri (top-level), matplotlib.tri.trifinder (top-level), matplotlib.tri.tritools (top-level), matplotlib.tri.triinterpolate (top-level)
missing module named matplotlib.axes.Subplot - imported by matplotlib.axes (top-level), matplotlib.pyplot (top-level)
missing module named olefile - imported by PIL.MicImagePlugin (top-level), PIL.FpxImagePlugin (top-level)
missing module named UserDict - imported by PIL.PdfParser (optional)
missing module named Tkinter - imported by PIL.ImageTk (conditional)
missing module named 'PySide.QtCore' - imported by PIL.ImageQt (conditional, optional)
missing module named 'PyQt4.QtCore' - imported by PIL.ImageQt (conditional, optional)
missing module named 'PySide2.QtCore' - imported by PIL.ImageQt (conditional, optional)
missing module named pathlib2 - imported by PIL.Image (optional)
missing module named cffi - imported by PIL.Image (optional), PIL.PyAccess (top-level), win32ctypes.core (optional), PIL.ImageTk (delayed, conditional, optional)


你是把文件放在主目录而不是dist文件夹里对吧?这就是整个代码了吗?那我会试着用pyinstaller来运行它,看看是否能成功。 - Delrius Euphoria
1
我不确定你所指的"文件"是什么,但这是整个代码库的全部内容。 - John Keith
通过文件,我指的是 .exe 文件。 - Delrius Euphoria
6个回答

8
你可以尝试通过安装旧版本的matplotlib软件包来解决此问题。 例如:
pip install matplotlib==3.2.2

我不能说这是一个很好的答案(无论它是什么,都没有解决根本问题),但是如果它不起作用,那我就完了。 - Urchin
1
如果我记得正确,我曾经尝试过将其降至3.1.x,但从未成功。今天我会再试一次。 - John Keith
2
原来,在matplotlib 3.3中,有一个环境变量被删除了,导致exe崩溃。MATPLOTLIBDATA环境变量在Matplotlib 3.1中已被弃用,并将在3.3中删除。降级到3.2.2显然是我的问题所在。我还升级了pyinstaller到4.0.dev。 - John Keith

6

1
谢谢。问题已解决。我为这个问题苦苦挣扎了很久。 - Vivek

0

我遇到了同样的问题:在另一个conda环境中工作时,出现了RuntimeError: Could not find the matplotlib data files。

因此,我参考了我的基本环境设置来解决工作环境中的问题:

Matplotlib版本为3.1.1,PyInstaller版本为3.6。我的Python版本是3.7.7。

pip install matplotlib==3.1.1
pip install pyinstaller==3.6

0

您可以继续使用当前的matplotlib版本,但必须更新 pyinstaller (5.0.dev0):

pip install -U https://github.com/pyinstaller/pyinstaller/archive/develop.zip


0

我尝试按照其他人建议的更改hook-matplotlib.py文件,但最新版本的Pyinstaller已经进行了这些更改,所以只要您的pyinstaller已更新,那么在这方面应该没问题(通过运行pip install pyinstaller --upgrade来检查)

然而,我仍然遇到了“无法确定matplotlib数据目录!”的问题,并成功解决了它:

  1. 运行pip uninstall pathlib

这是一个奇怪的问题,但不幸的是,在Python世界中有两个pathlib包:一个内置的和一个不同的PyPi包。如果您碰巧使用的是PyPi包,则hook-matplotlib.py中的get_data_path()函数无法按预期工作,并会导致断言失败。

  1. 在Pyinstaller命令中,包括--hidden-import matplotlib

不确定这个是否必须,但我在我的pyinstaller命令中包含了它,以确保使用matplotlib hook文件(例如:pyinstaller --onefile --windowed --hidden-import "matplotlib" ...)。

希望这对你有所帮助,这个解决方案真的很让人沮丧。


0

我遇到了相同的错误:

 NameError: name 'defaultParams' is not defined

我通过使用pyinstaller 4.0(开发版本)解决了上述错误。

您可以在此处下载pyinstaller开发版本: https://github.com/pyinstaller/pyinstaller

下载后,您可以像下面这样使用pip进行安装:

cd [download dir]  #change directory to donwloaded and unziped dir
pip install .

谢谢!我会尝试一下并告诉你它是否有效! - John Keith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接