[mod_python] PythonOption access

Graham Dumpleton grahamd at dscpl.com.au
Thu Jan 20 17:37:45 EST 2005

Bo Lorentsen wrote ..
> Hi ...
> I am a quite happy mod_python user (writting a payment system), and I 
> really like the combination of a OO language like python and a web 
> server like apache2. There have only been two real issues, I have been
> annoyed about. One is the missing possibility to reload a imported 
> module on update, and the other is the PythonOption access.
> Will it be possible to access PythonOptions like log_error in a more 
> global context (log_error is a function on the apache module), so that
> global values can get there init value on module init and NOT on the 
> first incomming request ? I have some code that work around this, but 
> its not pretty, and I like things to be so, and I don't understand why
> it has to be a method on the request module.
> Is this completely impossible ?

If I understand your issue with PythonOption correctly, the underlying
reason that get_options() is only available through the request object
is most likely that the set of options returned is dependent on the URL
accessed. That is, they aren't strictly global and thus fixed.

Take for example a .htaccess file containing:

  PythonOption A 1
  PythonOption B 2

If a URL request maps to a file in that directory, get_options() will return:

  A =1 
  B =2

If you have a subdirectory with another .htaccess file containing:

  PythonOption B 3
  PythonOption C 4

If a URL request maps to a file in that subdirectory, get_options() will

  A = 1
  B = 3
  C = 4

That is, the options from the parent directory are first inherited and
then overridden by any local to that directory sharing the same
name. Thus, all dependent on context and not global and fixed.

In terms of how to have global fixed options, what most people
probably do is have a special config module somewhere which
defines all their options as standard Python variables. Thus your
module simply imports the config module and accesses the

As to module reloading, there is the apache.import_module()
method. This is used to load the content handler defined by the
PythonHandler directive and if auto reload is turned on it will
be reloaded if that Python file changes. It will not however reload
a top level content handler if what is changed is something that
that content handler had in turn imported using "import".

One can use import_module() in a content handler instead of
"import", but needs to be done inside of actual handler() method
and there are still various problems with doing that. Vampire
tries to do a bit better job as far as the module importing system
goes to avoid these problems. It may or may not be useful to you.

If you want to explore the options better, you would be best off
posting some code examples of how you are achieving what you
want now.


More information about the Mod_python mailing list