【问题标题】:using py2exe with wxPython and Matplotlib将 py2exe 与 wxPython 和 Matplotlib 一起使用
【发布时间】:2011-09-23 05:38:18
【问题描述】:

我正在尝试从一个使用 wxPython 和 Matplotlib 的 python 脚本生成一个 .exe 文件,这似乎是不可能的。

我正在做的导入(与 Matplotlib 相关)如下:

from numpy import *
import matplotlib
matplotlib.interactive(True)
matplotlib.use("WXAgg")
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigCanvas
from matplotlib.ticker import MultipleLocator

这是我尝试使用的 setup.py 文件:

from distutils.core import setup
import py2exe
import matplotlib

opts = {
'py2exe': {"bundle_files" : 3,
           "includes" : [ "matplotlib", 
            "matplotlib.backends",  
            "matplotlib.backends.backend_wxagg",
                        "numpy", 
                        "matplotlib.ticker",
                        "matplotlib.figure", "_wxagg"],
            'excludes': ['_gtkagg', '_tkagg', '_agg2', 
                        '_cairo', '_cocoaagg',
                        '_fltkagg', '_gtk', '_gtkcairo', ],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                        'libgobject-2.0-0.dll']
          }
   }

setup(


  windows=[{'script':'starHunter.py', 'icon_resources':[(1, 'icon.ico')]}],

  data_files=matplotlib.get_py2exe_datafiles(),

  options=opts,

  zipfile=None
)

在尝试运行 .exe 文件后,我总是收到“找不到 matplotlib 数据文件”,顺便说一下,该文件已成功创建。

附加信息:我在 Windows XP 上使用 Python 2.6、Matplotlib 0.99.3、wxPython 2.8.11.0

提前致谢。 任何帮助将不胜感激!

干杯, 安德烈萨·西沃莱拉

【问题讨论】:

    标签: windows-xp wxpython matplotlib py2exe


    【解决方案1】:

    Py2exe 文档解释问题的根源并给出解决方案。它对我有用。 (matplotlib 版本 1.1.0,Python 2.7)

    http://www.py2exe.org/index.cgi/MatPlotLib

    由于我无权评论或评估其他答案,我必须自己写一个。柯克的回答对我来说是最有价值的帮助。 PyInstaller 可能是一种解决方法(尚未测试),但绝对不是问题的技术解决方案!

    from distutils.core import setup
    import py2exe
    from distutils.filelist import findall
    import os
    import matplotlib
    matplotlibdatadir = matplotlib.get_data_path()
    matplotlibdata = findall(matplotlibdatadir)
    matplotlibdata_files = []
    for f in matplotlibdata:
        dirname = os.path.join('matplotlibdata', f[len(matplotlibdatadir)+1:])
        matplotlibdata_files.append((os.path.split(dirname)[0], [f]))
    
    
    setup(
        console=['test.py'],
        options={
                 'py2exe': {
                            'includes': ["sip", "PyQt4.QtGui"],
                            'packages' : ['matplotlib', 'pytz'],
                            'excludes': ['_gtkagg', '_tkagg']
                           }
                },
        data_files=matplotlibdata_files
    )
    

    【讨论】:

      【解决方案2】:

      对于简单的测试,您可以简单地将 'site-packages\matplotlib' 中的 'mpl-data' 文件夹复制到您的应用程序文件夹中。据我所知,“mpl-data”不能捆绑到单个可执行文件中,因此它必须作为文件夹包含在您的二进制分发中。

      我通过 GUI2Exe 使用了 py2exe,并且可以冻结我使用 matplotlib + numpy/scipy + wx(很明显是 wxagg 后端)的应用程序。我不需要包含 _tkagg(在适用于我的 GUI2Exe 默认设置中明确排除了它)。

      【讨论】:

        【解决方案3】:

        尝试使用PyInstaller 而不是 py2exe。它完全支持 wxPython 和 matplotlib。与 py2exe 不同,它正在积极开发中。

        【讨论】:

        • 我支持这个建议。 PyInstaller 非常适用于 wxPython 和 matplotlib,还有一些在他们的 Supported Packages 列表中未提及的其他工具,例如 xlrd。在过去的几周里,我一直在一个项目中使用 PyInstaller 和所有 3 个软件包,而且几乎没有痛苦。
        • @ChrisC 我也使用过cx_freeze,因为 PyInstaller 1.4 不支持 Python 2.6。我有点惊讶 py2exe 自 2008 年以来一直没有更新,所以它继续被大量使用。
        • 老实说,很长一段时间我都没有看到 py2exe 和 PyInstaller 之间有什么区别,只是默认使用 py2exe ......直到我发现 PyInstaller 处理了 MSVCR*.DLL 分发愚蠢并且可以包装 matplotlib。从那以后再也没有回头。
        • 你绝对可以使用py2exe提供带有matplotlib的包;见 otterb 的回答。
        • pyinstaller 比 py2exe 好很多!感谢您的提示!
        【解决方案4】:

        matplotlib.get_py2exe_datafiles() 存在许多问题,如果它工作起来会很方便。指定要使用的后端也是一个好主意。这是我最近使用的一个工作 matplotlib 导入:

        from distutils.core import setup
        import py2exe
        from glob import glob
        
        import matplotlib       #Import then use get_py2exe_datafiles() to collect numpy datafiles.
        matplotlib.use('wxagg') #Specify matplotlib backend. tkagg must still be included else error is thrown.
        
        data_files = [
                    ("Stuff", glob(r'C:\ProjectFolder\Stuff\*.*')) 
                    ,("dlls", glob(r'C:\ProjectFolder\dlls\*.dll'))  
                    ,("pyds", glob(r'C:\ProjectFolder\pyds\*.pyd')) # py2exe specified pyd's 
                    ]
        # Extend the tuple list because matplotlib returns a tuple list.      
        data_files.extend(matplotlib.get_py2exe_datafiles())  #Matplotlib - pulls it's own files
        
        options =   {'py2exe':{#'bundle_files': 1,                                 # Bundle files to exe
                                'includes': ["matplotlib.backends.backend_tkagg"]  # Specifically include missing modules
                                ,'excludes': ['_gtkagg', 'tkagg']                  # Exclude dependencies. Reduce size.
                              }
                    }   
        
        setup(
        name='ProjectName'
        ,options = options  
        ,data_files=data_files
        ,console=['projectname.py']
        )
        

        【讨论】:

        • 非常感谢。我知道这个答案已经很老了,但我想我永远无法成功地将我的程序转换为 exe。我知道,“谢谢”在技术上不是一个有效的评论,但这真的对我很有帮助。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-31
        • 1970-01-01
        • 2022-08-06
        • 2017-09-14
        相关资源
        最近更新 更多