Differences between revisions 6 and 7
Revision 6 as of 2014-09-08 19:52:55
Size: 449
Editor: LZJN
Comment:
Revision 7 as of 2014-09-09 16:10:14
Size: 2146
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Hello! My name is Leandro. <<BR>>
It is a little about myself: I live in Switzerland, my city of Hagenbuch. <<BR>>
It's called often Northern or cultural capital of . I've married 2 years ago.<<BR>>
I have two children - a son (Jacques) and the daughter (Irvin). We all like Amateur geology.<<BR>>
<<BR>>
Feel free to visit my blog post [[http://www.amazon.com/Awareness-Classic-Silver-Crystal-Bracelet/dp/B00J4RSPOI/|silver autism bracelet]]
= The Problem =

I use [[http://www.pythonware.com/products/pil/|PIL]] from pythonware. It has some very dynamic initialising routines.

excerpt out of Image.py
{{{
#!python
def init():
    "Load all file format drivers."

    global _initialized
    if _initialized >= 2:
        return

    visited = {}

    directories = sys.path

    try:
        directories = directories + [os.path.dirname(__file__)]
    except NameError:
        pass

    # only check directories (including current, if present in the path)
    for directory in filter(isDirectory, directories):
        fullpath = os.path.abspath(directory)
        if visited.has_key(fullpath):
            continue
        for file in os.listdir(directory):
            if file[-14:] == "ImagePlugin.py":
                f, e = os.path.splitext(file)
                try:
                    sys.path.insert(0, directory)
                    try:
                        __import__(f, globals(), locals(), [])
                    finally:
                        del sys.path[0]
                except ImportError:
                    if DEBUG:
                        print "Image: failed to import",
                        print f, ":", sys.exc_value
        visited[fullpath] = None

    if OPEN or SAVE:
        _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.

{{{
#!python
# PIL schafft das nicht alleine, wenn gepy2exed
import Image
import PngImagePlugin
[...]
Image._initialized=2
}}}

before using PIL, I manually import Image and also the needed *ImagePlugins
{{{
#!python
[...] could be JpegImagePlugin, BmpImagePlugin etc.
}}}
Finally I force Image to believe it is fully initialized, so nobody tries directory scanning.

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)