[mod_python] The DOs and DONTs of mod_python: one problem found

Graham Dumpleton grahamd at dscpl.com.au
Tue Oct 18 21:26:24 EDT 2005


Nic Ferrier wrote ..
> "Graham Dumpleton" <grahamd at dscpl.com.au> writes:
> 
> > Also be aware that the current working directory of Apache is not
> > guaranteed. Thus, when a handler is executed, the current working
> > directory WILL NOT be that of where the handler code resides. It
> > is therefore very important to use absolute pathnames to resources
> > you need to access. If such resource files are located in proximity
> > to the handler code file, best to use something like:
> >
> >   import os
> >   __here__ = os.path.dirname(__file__)
> 
> Isn't a better approach to abstract out the fixed file refs and pass
> in docroot as a reference?
> 
>   def handler(http):
>      docroot = http.document_root()
>      do_some_call(docroot)

Depends on your setup. Other setups may not stick everything under the
true document root, but locate it elsewhere using an Alias directive.
You also have the issue of UserDir accounts, eg. "/~user/some/path".

Thus, basing it on where the code file is, is sometimes the simplest thing
to do.

The other alternative one can often use is:

  req.hlist.directory

This will be set to the pathname of the directory for which the
PythonHandler directive (or other handler dependent on phase) was
defined. This one is good to use where you have numerous applications
hosted in the one server, each with their own PythonHandler directive.
In essence it gives you a local root for that application.

Graham


More information about the Mod_python mailing list