[mod_python] Accessing objects loaded with PythonImport

Nicolas Lehuen nicolas.lehuen at gmail.com
Mon May 30 17:00:21 EDT 2005


Also note that if the site_setup module is changed, it won't be
automatically reloaded unless xmlhandler.py is also changed. Plus, if
you have many site_setup.py in different directories, they will
collide with each other.

The dirt simple solution to the collision problem is simply to put
site_setup.py in a separate directory (not one accessible from
Apache), put the directory on the PYTHONPATH environment variable and
simply use a standard "import site_setup" instruction. But then, your
module won't be automagically reloaded when modified...

Note that mod_python 3.1.x is purposedly quite low-level. It does not
provide a high-level application framework, so things like data or
code sharing between pages are not handled. You should either build
your own framework or use one like Vampire
(http://www.dscpl.com.au/projects/vampire/). Meanwhile, we are trying
to provide a few more stepping stones in the upcoming 3.2.0 release.

Regards,
Nicolas

2005/5/30, Nicolas Lehuen <nicolas.lehuen at gmail.com>:
> Hi Lee,
> 
> Unfortunately the answer is not as simple as it seems.
> 
> Try doing this in xmlhandler.py :
> 
> from mod_python import apache
> 
> from os.path import dirname
> this_directory = dirname(__file__)
> site_setup = apache.import_module("site_setup",path=[this_directory])
> 
> check = str(dir())
> 
> def handler(req):
>            ...
>            req.write(req.interpreter + '<br />')
>            req.write(check)
>            ...
>            return apache.OK
> 
> We use apache.import_module to import the site_setup module, looking
> in the current directory for site_setup.py[cd].
> 
> Regards,
> Nicolas
> 
> 2005/5/30, Lee Brown <administrator at leebrown.org>:
> > Greetings!
> >
> > I am trying to use the PythonImport directive to perform some initialization
> > routines for each vhost on server startup and then have my request handler
> > for that vhost access data and/or objects from those initial routines.
> > Unfortunately, I cannot for the life of me find the namespace in which these
> > objects reside from within the handler.  Here is what's going on:
> >
> > Platform:
> >
> >         Apache/2.0.52 (Win32) mod_python/3.1.3 Python/2.3.4
> >
> > In the 'main' section of httpd.conf:
> >
> >         ...
> >         AddHandler mod_python .py
> >         PythonPath
> > "sys.path+['c:/projects/webdev/sites/crashtest/config','c:/projects/webdev/s
> > ites/crashtest/home/xmltest']"
> >         PythonImport site_setup.py crashtest
> >         ...
> >
> > In the 'vhost' section of httpd.conf:
> >
> >         <VirtualHost *:80>
> >                 ServerName crashtest.leebrown.org
> >                 ...
> >                 PythonInterpreter crashtest
> >                 PythonDebug On
> >                 ...
> >                 <Directory
> > "C:/Projects/webdev/sites/crashtest/home/xmltest">
> >                         ...
> >                         PythonHandler xmlhandler
> >                         DirectoryIndex xmlhandler.py
> >                         ...
> >                 </Directory>
> >                 ...
> >         </VirtualHost>
> >
> > File site_setup.py (in its entirety):
> >
> >         magic_string = 'Ooggaa-Booggaa!'
> >
> > Finally, the file xml_handler.py:
> >
> >         from mod_python import apache
> >
> >         check = str(dir())
> >
> >         def handler(req):
> >                 ...
> >                 req.write(req.interpreter + '<br />')
> >                 req.write(check)
> >                 ...
> >                 return apache.OK
> >
> > A user agent request to the local URL 'crashtest.leebrown.org/xmltest'
> > returns the following:
> >
> >         crashtest
> >         ['__builtins__', '__doc__', '__file__', '__mtime__', '__name__',
> > 'apache', 'check', 'handler']
> >
> > 'crashtest' is the interpreter name I expected to get, however neither
> > 'site_setup' nor 'magic_string' seems to be available to my handler
> > function.  I've explored around using the dir() function with magic_string,
> > site_setup.magic_string, __name__, etc., etc. without success.
> >
> > I suspect that the answer is dirt simple, but I'll be horn-swaggled if I can
> > find it.
> >
> > Best Regards,
> > Lee E. Brown
> > (leebrown at leebrown.org)
> >
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> >
>



More information about the Mod_python mailing list