[Courtland Idstrom]
I'm dealing with a few external methods that I prefer to keep in separate files. There are a few shared routines that I need to import from them, but unfortunately I can't find a way to import things in the Extensions directory (for example I need to import a few things from a files called shared.py). The only way I've successfully gotten this to work is by adding the current path to Extensions to sys.path before 'import shared'. I'd rather not do it this way in case my zope installation moves paths/servers, etc.
Notice that the usual __path__ and __file__ properties from python seem to be inaccessible, so I can't add it to sys.path in a way that's going to work in the future.
Although putting an __init__.py onto the extensions directory might be made to work, I do not recommend it. Instead, put the python code somewhere else on the python path, outside the Zope tree. That way you can maintain the code separately, and if you replace your Zope installation, you do not have to remember to do anything beyond setting the python path of the new Zope installation. This approach also keeps the extensions directory uncluttered, which I think is very desirable. The best way to put a new directory on the python path, I think, is with a .pth file. Put it in the same directory as the python executable that Zope uses, and put the path to the directory on a single line. You can have as many such paths as you want in one .pth file. The external method should just dispatch to the real code and do nothing else besides returning results to Zope. For example, here is my external method for taking a string containing XML and transforming it with an xslt stylesheet, using 4suite: from fourxslt.do_xslt import simpleXslt def xslt_xmlstring(xml,xslt): '''Transform an xml string using a stylesheet file.''' if not (xml and xslt): return '' result=simpleXslt(str(xml),xslt) return result Here, "simpleXslt" is a wrapper function I wrote around the 4xslt code. The "xml" parameter is supposed to be a string containing xml, and the "xslt" value is the path to the stylesheet. The module "fourxslt" is on my python path, courtesy of a .pth file. I compute the path in Zope by getting the base directory using another external method call, then concatenating it with the right stylesheet name. You can set this up in an __init__.py file for a package so that the basepath gets imported when the package is imported. This lets you use only relative paths, which is a very good idea because it lets you move your code to other locations and other machines without changing the paths. One drawback to this approach is that you may have to stop and restart Zope when you change the code that is outside of the external method. The best way to handle that is to make the code self-testing without using Zope at all, although I know that is not always possible. I think this tradeoff is worth it. Cheers, Tom P