Jim Gallacher
jpg at jgassociates.ca
Fri Dec 1 11:09:06 EST 2006
Clodoaldo wrote: > 2006/12/1, Graham Dumpleton <grahamd at dscpl.com.au>: >> >> The third option is to not use a package. If you created the package >> merely >> out of convenience to provide a namespace for the modules, this should >> be okay. Thus, remove the empty __init__.py file so that it isn't >> actually a >> package anymore. Then, change: >> >> from lib import HTMLBuilder >> >> to: >> >> from mod_python import apache >> >> HTMLBuilder = apache.import_module('~/lib/HTMLBuilder.py') >> >> This is using the mod_python module importer directly to import the >> module >> by path. The '~/' prefix to the path is special and is understood by the >> module importer to be the directory that the active Python*Handler >> directive >> was specified for. >> >> >> A fourth option is to again remove the package __init__.py file so it >> isn't >> really a package anymore, but also move the directory to somewhere >> outside >> of the document tree. In order to find the modules though, the PythonPath >> directive is NOT used and instead the new module importers own search >> path is set. For example: >> >> PythonOption mod_python.importer.path "['/some/path/lib']" >> >> The module importer path SHOULD NOT mention sys.path, it should instead >> be a distinct list of directories where only the new module importer >> will look. >> Because the 'lib' directory is include and the contents of the >> directory will be >> searched, can just say: >> >> import HTMLBuilder >> >> If for some reason you wanted to do it this way, but not move the >> directory >> outside of the document tree, you could also say: >> >> PythonOption mod_python.importer.path "['~/lib']" >> >> Here the '~/' prefix is again used so you don't have to have an >> absolute path. >> When used it will be expanded to the directory the handler was >> specified for. >> >> >> Anyway, hope I haven't confused you too much. >> > > Your message is not confusing. It came in a good time and it clears > some confusion i had. But something is not working for me. Using > mod_python 3.3 in FC6. > > I'm trying to import a module with both the third and fourth > techniques you described but I'm having errors. > > This is the Virtual host config: > > ServerName carconsumption.s0 > DocumentRoot /var/www/html/carconsumption.com > SetHandler mod_python > PythonHandler mod_python.publisher > PythonDebug On > PythonOption mod_python.importer.path "['~/mod']" > PythonImport db_connection carconsumption.s0 > > The module "constants" is in the mod directory under the document root: > > [cpn at s0 mod]$ pwd > /var/www/html/carconsumption.com/mod > [cpn at s0 mod]$ ll constants.py > -rwxr----- 1 cpn apache 28675 Nov 29 17:42 constants.py ^^^^^ possible permission problem - apache can't read this file unless your apache process is running as user cpn. > [cpn at s0 mod]$ ll -d ../mod > drwxrws--- 2 cpn apache 4096 Dec 1 12:26 ../mod > > > ImportError: No module named ~/mod/constants.py Which makes sense - if apache can't read it, mod_python can't import it. Jim
|