将 py2exe 与 wxPython 和 Matplotlib 结合使用

发布于 2024-11-17 18:34:09 字数 1443 浏览 3 评论 0原文

我正在尝试从使用 wxPython 和 Matplotlib 的 python 脚本生成 .exe 文件,但这看起来是不可能的。

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

from numpy import *
导入 matplotlib
matplotlib.interactive(True)
matplotlib.use("WXAgg")
从 matplotlib.figure 导入图
从 matplotlib.backends.backend_wxagg 导入FigureCanvasWxAgg 作为 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

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

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

I'm trying to generate an .exe file from a python script that uses wxPython and Matplotlib and it looks like to be impossible.

The imports I'm doing (related with Matplotlib) are the following:

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

Here is the setup.py file I'm trying to use:

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
)

I'm always getting "Could not find matplotlib data files" after trying to run the .exe file, which by the way, is successfully created.

Additional information: I'm using Python 2.6, Matplotlib 0.99.3, wxPython 2.8.11.0 on Windows XP

Thanks in advance.
Any help will be appreciated!

Cheers,
Andressa Sivolella

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘦怹 2024-11-24 18:34:09

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

Try using PyInstaller rather than py2exe. It has full support for wxPython and matplotlib. And it's in active development, unlike py2exe.

饮湿 2024-11-24 18:34:09

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']
)

There are a number of problems with matplotlib.get_py2exe_datafiles(), as convenient as it would be if it worked. It's also a good idea to specify which backend to use. Here's a working matplotlib import I recently used:

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']
)
若能看破又如何 2024-11-24 18:34:09

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

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

For a simply test, you could simply copy 'mpl-data' folder in 'site-packages\matplotlib' to your app folder. As far as I know, 'mpl-data' cannot be bundled into the single executable so this has to be included in your binary distribution as a folder.

I used py2exe via GUI2Exe and could freeze my app that uses matplotlib + numpy/scipy + wx (so obviously wxagg backend). I didn't need to include _tkagg (which is explicitly excluded in GUI2Exe default setting which worked for me).

小苏打饼 2024-11-24 18:34:09

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
)

Py2exe documentation explains the source of the problem and give solutions. It worked for me. (matplotlib version 1.1.0, Python 2.7)

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

Since I have no privilege to comment or evaluate other answers, I have to write my own one. Kirk's answer was the most valuable help for me. PyInstaller might be a workaround (have not tested it) but is definitely not the technical solution to the problem !

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
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文