Slithy home


At the moment this is broken; py2exe won't work on Slithy presentations due to some DLL conflicts. I'm working on it...

packaging up your presentation

There is a package called py2exe which can take a Python script and package it up along with any other scripts it imports and a Python interpreter into a single .exe. It will also find any DLLs your script depends on and copy them too. This means Python and all the related libraries don't have to be installed on your presentation machine — you just unzip a directory, double-click the .exe file, and go.

Here I will go over the steps you need to take your script and package it up. I will assume that your presentation directory is structured something like this:

mytalk/
   mainscript.py     <-- the main presentation script
   sub1.py
   sub2.py
   gldiagram.pyd
   . . .
   fonts/
      whatever.pfb
      other.ttf
      foo.slf
      . . .
   images/
      foo.jpg
      bar.tga
      baz.gif
      qux.png
      movie.avi

That is, all your scripts and OpenGL diagrams (if any) are in the main directory, and there are subdirectories containing other files (fonts and images) loaded by your presentation.

It's not essential that your presentation looks exactly like this. What is essential is that all images and font files are loaded using relative paths, not absolute paths. Once it's rolled up into an EXE, you won't be able to go in and change these, so make sure they are specified relative to the location of the main script!

Run through your presentation to confirm that it works. Once you're ready you can begin the packaging process:

  1. Install py2exe. This only needs to be done once on a machine. Here's the link: py2exe-0.3.3.win32-py2.2.exe — it comes with a pretty Windows installer and everything.

  2. Make a static version of Pmw. Slithy uses a package called Pmw which normally dynamically loads pieces of itself as needed. This doesn't work with py2exe, so we need to fix it. You'll only need to do this once for your presentation; if you make a change to your script and repackage you can skip this step.

  3. Statically load PIL image formats. Slithy uses the Python Imaging Library to read image files. Normally PIL dynamically loads plugins for the different file formats as it needs them; this scheme won't work with py2exe.

    At the top of your main script, add a blob of code that looks like this:

    import Image
    
    import JpegImagePlugin    # import drivers for every image format you use
    import TgaImagePlugin
    import PngImagePlugin
    import GifImagePlugin
    
    Image._initialized = 1
    

    This will force the import of the formats you list. You need to load the plugins for every image type that's used in your presentation. Here's the complete list of available plugins:

    ArgImagePlugin BmpImagePlugin CurImagePlugin
    DcxImagePlugin EpsImagePlugin FliImagePlugin
    FpxImagePlugin GbrImagePlugin GifImagePlugin
    IcoImagePlugin ImImagePlugin ImtImagePlugin
    IptcImagePlugin JpegImagePlugin McIdasImagePlugin
    MicImagePlugin MpegImagePlugin MspImagePlugin
    PcdImagePlugin PcxImagePlugin PdfImagePlugin
    PixarImagePlugin PngImagePlugin PpmImagePlugin
    PsdImagePlugin SgiImagePlugin SunImagePlugin
    TgaImagePlugin TiffImagePlugin WmfImagePlugin
    XVThumbImagePlugin XbmImagePlugin XpmImagePlugin

  4. Create a setup script. In your script directory, create a new script setup.py that contains the following:

    from distutils.core import setup
    import py2exe
    
    setup(name="presentation",
          scripts=["mainscript.py"],    # put the name of your main script here
    )
    

  5. Create the .exe. In your script directory, execute the following command:

    % python setup.py py2exe
    

    It prints lots of stuff as it searches for dependencies, byte-compiles files, etc. You'll probably get a warning at the end about it not locating the "os.path" module, but this is harmless.

    When it finishes, your directory structure will look like this:

    mytalk/
       mainscript.py     <-- the main presentation script
       . . .
       fonts/
          . . .
       images/
          . . .
       build/      # all this is created by running setup.py
          . . .
       dist/
          mainscript/
              mainscript.exe
    	  *.dll
    	  *.pyd	  
    

    The leftover build directory is superfluous; you can ignore it or delete it if you like.

  6. Copy other files your presentation needs. Copy any image and font files your presentation needs to the mainscript directory, making sure they're in the same position relative to mainscript.exe that they were to mainscript.py. Here's what my example looks like after the copy:

    mytalk/
       mainscript.py     <-- the main presentation script
       . . .
       fonts/
          . . .
       images/
          . . .
       build/
          . . .
       dist/
          mainscript/           # zip up this part of the tree
              mainscript.exe    # to create a portable presentation
    	  *.dll
    	  *.pyd	  
              fonts/      <-- copy of the original fonts dir
                 . . .
              images/     <-- copy of the original images dir
                 . . .
    

    If you're planning to distribute your presentation, keep in mind the legal implications of distributing scalable outline font files. You may want to use Slithy's own image-based font format so as to avoid distributing TTF and PFB files.

  7. Make a zip file. At this point you should be able to go into the mainscript directory and run mainscript.exe to display your presentation. After confirming that it works, zip up the entire mainscript directory (the boldface part of the tree above.) This is your whole presentation, and should work even on machines without Python installed.

As always, let me know if something's not working for you.