The Problem

I use PIL from pythonware. It has some very dynamic initialising routines.

excerpt out of Image.py

   1 def init():
   2     "Load all file format drivers."
   3 
   4     global _initialized
   5     if _initialized >= 2:
   6         return
   7 
   8     visited = {}
   9 
  10     directories = sys.path
  11 
  12     try:
  13         directories = directories + [os.path.dirname(__file__)]
  14     except NameError:
  15         pass
  16 
  17     # only check directories (including current, if present in the path)
  18     for directory in filter(isDirectory, directories):
  19         fullpath = os.path.abspath(directory)
  20         if visited.has_key(fullpath):
  21             continue
  22         for file in os.listdir(directory):
  23             if file[-14:] == "ImagePlugin.py":
  24                 f, e = os.path.splitext(file)
  25                 try:
  26                     sys.path.insert(0, directory)
  27                     try:
  28                         __import__(f, globals(), locals(), [])
  29                     finally:
  30                         del sys.path[0]
  31                 except ImportError:
  32                     if DEBUG:
  33                         print "Image: failed to import",
  34                         print f, ":", sys.exc_value
  35         visited[fullpath] = None
  36 
  37     if OPEN or SAVE:
  38         _initialized = 2

main problem with all this stuff: all this directory-voodoo is out of access when using py2exe. Maybe it is fixable by doing very strange things I do not know about.

Explanation

PIL tries to automagically learn which Image-Plugins are available. "available" is well defined as long it is a Python standard installation. "available" gets very vague when using py2exe.

Solution

Make it dumb.

   1 # PIL schafft das nicht alleine, wenn gepy2exed
   2 import Image
   3 import PngImagePlugin
   4 [...]
   5 Image._initialized=2

before using PIL, I manually import Image and also the needed *ImagePlugins

   1 [...] could be JpegImagePlugin, BmpImagePlugin etc.

Finally I force Image to believe it is fully initialized, so nobody tries directory scanning.

py2exeAndPIL (last edited 2014-09-09 16:10:14 by JimmyRetzlaff)