py2exe + matplotlib == frustration

Hey man… you are great… thanks a lot for your valuable help!!!

···

On 6/17/07, Werner F. Bruhin <werner.bruhin@…185…> wrote:

Hi,

Viraj Vajratkar wrote:

Hi… thanks a lot for replying… but it would be great if you could
send me your complete setup.py file… thanks a lot in advance…
Attached is a setup.py and the corresponding the matplotlib example

Werner

-- coding: iso-8859-1 --#

from distutils.core import setup
import os
from os.path import join
import shutil

import glob
import py2exe
from py2exe.build_exe import py2exe

import sys

import matplotlib as mp
matplotlib_font_afm = glob.glob(os.sep.join([mp.get_data_path(), ‘fonts/afm/']))
matplotlib_font_pdfcorefonts = glob.glob(os.sep.join([mp.get_data_path(), 'fonts/pdfcorefonts/
’]))

matplotlib_font_ttf = glob.glob(os.sep.join([mp.get_data_path(), ‘fonts/ttf/']))
matplotlib_images = glob.glob(os.sep.join([mp.get_data_path(), 'images/
’]))

following should not be needed as of py2exe 0.6.6

cleanup dist and build directory first (for new py2exe version)

##if os.path.exists(“dist/prog”):

shutil.rmtree(“dist/prog”)

##if os.path.exists(“dist/lib”):

shutil.rmtree(“dist/lib”)

##if os.path.exists(“build”):

shutil.rmtree(“build”)

A program using wxPython

The manifest will be inserted as resource into the .exe. This

gives the controls the Windows XP appearance (if run on XP :wink:

manifest_template = ‘’’

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>


<assemblyIdentity
version=“5.0.0.0
processorArchitecture=“x86”
name=“%(prog)s”
type=“win32”

/>
%(prog)s


<assemblyIdentity
type=“win32”
name=“Microsoft.Windows.Common-Controls
"
version=“6.0.0.0
processorArchitecture=“X86”
publicKeyToken=“6595b64144ccf1df”
language=”*"

    />
</dependentAssembly>
<requestedExecutionLevel
      level="AsInvoker"
      uiAccess="false"/>
    </requestedPrivileges>
   </security>
'''

RT_MANIFEST = 32

options for py2exe

options = {“py2exe”: {“compressed”: 1,
“optimize”: 2,
“packages”: [“encodings”,

                               "pytz", "matplotlib.numerix",
                               ],
                  "excludes": ["MySQLdb", "Tkconstants", "Tkinter", "tcl"

                  ],
                  "dll_excludes": ["tcl84.dll", "tk84.dll", "wxmsw26uh_vc.dll"]
                  }
      }

zipfile = r"lib\library.zip"

class MetaBase:
def init(self, **kw):
self.dict.update(kw)
self.version = ‘1.0’
self.author = “yourname”
self.author_email = "
name@…724…"
self.company_name = “”
self.copyright = “2003 - 2007 by whoever”
self.url = "http://www.whatever.com/
"
self.download_url = “http://www.whatever.com/en/
self.trademark = “”
self.comments = “a comment on the prog”

    [self.name](http://self.name) = "the prog name"
    self.description = "a desc on the prog"

wx_emb = MetaBase(
script = “embedding_in_wx.py”,

        other_resources = [(RT_MANIFEST, 1, manifest_template % dict(prog="your prog name"))],

icon_resources = [(1, r"images/some.ico")],

        dest_base = r"prog\wx_embed")

setup(
classifiers = [“Copyright:: your name”,
“Development Status :: 5 Stable”,
“Intended Audience :: End User”,
“License :: Shareware”,

                 "Operating System :: Microsoft :: Windows 2000",
                 "Operating System :: Microsoft :: Windows XP",
                 "Operating System :: Microsoft :: Windows 9x",

                 "Programming Language :: Python, wxPython",
                 "Topic :: Home Use"
                 "Natural Language :: German",
                 "Natural Language :: French",

                 "Natural Language :: English"],
  windows = [wx_emb],
  options = options,
  zipfile = zipfile,
  data_files = [("lib\\matplotlibdata", [os.sep.join

([mp.get_data_path(), ‘matplotlibrc’])]),
(“lib\matplotlibdata\fonts\afm”, matplotlib_font_afm),
(“lib\matplotlibdata\fonts\pdfcorefonts”, matplotlib_font_pdfcorefonts),

                ("lib\\matplotlibdata\\fonts\\ttf", matplotlib_font_ttf),
                ("lib\\matplotlibdata\\images", matplotlib_images),
                ]
)

#!/usr/bin/env python

embedding_in_wx.py

“”"
Copyright (C) Jeremy O’Donoghue, 2003

License: This work is licensed under the PSF. A copy should be included
with this source code, and is also available at

http://www.python.org/psf/license.html

This is a sample showing how to embed a matplotlib figure in a wxPanel.

The example implements the full navigation toolbar, so you can automatically

inherit standard matplotlib features such as the ability to zoom, pan and
save figures in the supported formats.

There are a few small complexities worth noting in the example:

  1. By default, a wxFrame can contain a toolbar (added with SetToolBar())

    but this is at the top of the frame. Matplotlib default is to put the
    controls at the bottom of the frame, so you have to manage the toolbar
    yourself. I have done this by putting the figure and toolbar into a

    sizer, but this means that you need to override GetToolBar for your
    wxFrame so that the figure manager can find the toolbar.

  2. I have implemented a figure manager to look after the plots and axes.

    If you don’t want a toolbar, it is simpler to add the figure directly
    and not worry. However, the figure manager looks after clipping of the
    figure contents, so you will need it if you want to navigate

  3. There is a bug in the way in which my copy of wxPython calculates
    toolbar width on Win32, so there is a tricky line to ensure that the
    width of the toolbat is the same as the width of the figure.

  4. Depending on the parameters you pass to the sizer, you can make the
    figure resizable or not.
    “”"

import matplotlib
matplotlib.use(‘WX’)
from matplotlib.backends.backend_wx import Toolbar, FigureCanvasWx,\

 FigureManager

from matplotlib.figure import Figure
from matplotlib.axes import Subplot
import matplotlib.numerix as numpy
import wx

class PlotFigure(wx.Frame):
def init(self):

    wx.Frame.__init__(self, None, -1, "Test embedded wxFigure")

    self.fig = Figure((9,8), 75)
    self.canvas = FigureCanvasWx(self, -1, self.fig)
    self.toolbar = Toolbar(self.canvas

)
self.toolbar.Realize()

    # On Windows, default frame size behaviour is incorrect
    # you don't need this under Linux
    tw, th = self.toolbar.GetSizeTuple()
    fw, fh = self.canvas.GetSizeTuple()
    self.toolbar.SetSize(wx.Size(fw, th))

    # Create a figure manager to manage things
    self.figmgr = FigureManager(self.canvas, 1, self)
    # Now put all into a sizer

    sizer = wx.BoxSizer(wx.VERTICAL)
    # This way of adding to sizer allows resizing
    sizer.Add(self.canvas, 1, wx.LEFT|wx.TOP|wx.GROW)
    # Best to allow the toolbar to resize!

sizer.Add(self.toolbar, 0, wx.GROW)
self.SetSizer(sizer)
self.Fit()

def plot_data(self):
    # Use ths line if using a toolbar
    a = self.fig.add_subplot(111)

    # Or this one if there is no toolbar

    #a = Subplot(self.fig, 111)

    t = numpy.arange(0.0,3.0,0.01)
    s = numpy.sin(2*numpy.pi*t)
    c = numpy.cos(2*numpy.pi*t)
    a.plot(t,s)
    a.plot(t,c)

self.toolbar.update()

def GetToolBar(self):
    # You will need to override GetToolBar if you are using an
    # unmanaged toolbar in your frame
    return self.toolbar

if name == ‘main’:

app = wx.PySimpleApp()
frame = PlotFigure()
frame.plot_data()
frame.Show()
app.MainLoop()