Python packages (was: RE: [Zope] FTDraw)

Dieter Maurer dieter@handshake.de
Thu, 15 Mar 2001 09:54:24 +0100 (CET)


Paz writes:
 > Thanks for the tip Dieter, but indeed, the __init__.py is there.
 > 
 > However, you say that the _init_ is necessary. Is this more of a zope thing?
It is being a Python thing:

  Python before 1.5 only knew "modules".

  Then, following Java, packages have been invented (to prevent
  pollution and conflicts in namespace, modules are found in).

  A package is essentially a (file system) folder that contains
  modules and other packages (subpackages).
  You use it in forms like this:

    import package.subpackage.module
  or
    from package.subpackage import module
   
  Details in Python's package support document.


  The problem with this: legacy

   * of course, modules have been organized before in folders.
     How to distinquish such folders from packages?

     Guido descided, that the existence of "__init__.py"
     does this. If it is there, the folder is a package;
     otherwise, it is just a folder containing modules.

   * "*.pth" files

     They have been invented to make modules organized in
     folders easily accessible be Zope.
     Some magic (defined in "site.py") looks for "*.pth" files
     and extends PYTHONPATH by what it finds there.
     PYTHONPATH is used to locate modules (and now packages).
     Through the PYTHONPATH extension, modules could be found
     even if they have been in subfolders.

  Using packages and "*.pth" files together can lead to serious
  problems (although, I do not expect this in your case):

     It confuses Python's module loader. Normally, it prevents
     the same module to be loaded twice. But under this
     confusion, it may load the same module several times
     under different names.

     Some modules hate to be loaded twice...


Your code snipped "import PIL.XXXX" with the error message
"could not find 'XXXX'" suggests, that "PIL" is used as a package
and that is has been found but "XXXX" was not found.
I cannot remember the precise name, but I think, your "XXXX"
should be defined there.

  Apparently, the PIL found is strange, not what/how it should
  be.

Usually, I would analyse such problems be starting the Python
interpreter interactively (without Zope).
Under Unix (I always work under Unix, can't help much with Windows):

	    export PYTHONHOME=<Your Zope Distribution Directory>
	    <Your Python used by Zope, usually "bin/python">
	    import PIL
	    PIL.__path__
	    PIL.__file__

If "PIL.__path__" raises an exception, your "PIL" is not a package.
"PIL.__file__" tells you, where the module/package has been found.

If you put PIL inside the "Products" folder (in fact a package),
then the above "import" would look like:

	    from Products import PIL


Dieter