[Image-SIG] using PIL as a package-PIL doesn't load all ImagePlugins-patch?

Christopher Lee clee@v1.wustl.edu
Sat, 14 Oct 2000 14:04:28 -0500


I'm trying to use PIL (Imaging-1.1) as a package, i.e., without adding the
PIL directory to the python path.

I appear to be having trouble because Image.py:: init() doesn't load the
plugins appearing in the PIL directory w/o it being on the path.

The code appears like this:

    for path in filter(os.path.isdir, sys.path):
	for file in os.listdir(path):
	    if file[-14:] == "ImagePlugin.py":
                 ## and so on ##

Thus, for example, the PngImagePlugin.py is missed because it's nolonger on
sys.path and it's not listed among the default plugins loaded in preinit().

I don't know if there a patch for this or a standard mechanism for dealing
with this.

I have fixed it "by hand" by enlarging the search to include the "PIL"
directory (my patch starts after "cwl") :

def init():
    "Load all file format drivers."

    global _initialized
    if _initialized >= 2:

    import os, sys

    # only check directories (including current, if present in the path)
    # cwl: have added directory that Image.py resides in to path search list
    tmppath = sys.path
    (directory, basename) = os.path.split(__file__)
    # print "directory is %s" % directory
    for path in filter(os.path.isdir, tmppath):
	for file in os.listdir(path):
	    if file[-14:] == "ImagePlugin.py":
		p, f = os.path.split(file)
		f, e = os.path.splitext(f)
		    sys.path.insert(0, path)
			__import__(f, globals(), locals(), [])
			del sys.path[0]
		except ImportError:
		    if DEBUG:
			print "Image: failed to import",
			print f, ":", sys.exc_value

    if OPEN or SAVE:
	_initialized = 2