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

Graham Dumpleton grahamd at dscpl.com.au
Tue Oct 18 20:19:57 EDT 2005

Philippe C. Martin wrote ..
> Hi,
> It took me a while to understand that although I had apache configured
> to run 
> as me "philippe", doing an os.getenv still returned the root environment
> variables.
> Maybe forcing a source of ~/.bashrc ?

Probably not a good idea. Use the SetEnv directive in Apache config
files to add extra environment variables. Eg.

  SetEnv LD_LIBRARY_PATH /usr/local/lib

At least I think that should work. May need to be at Apache global
configuration level.

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__)

The __here__ variable will be the name of the directory your handler
code file is in, you can then refer to resource files relative to this
base directory. By doing:

  resource = os.path.join(__here__,'templates/xxx.tmpl')

Doing this is better as it avoids absolute pathnames in as much as
the base directory is automatically determined from where the
handler code file resides.


More information about the Mod_python mailing list