[mod_python] example of the PythonImport directive

Graham Dumpleton grahamd at dscpl.com.au
Tue Dec 26 19:45:46 EST 2006

On 27/12/2006, at 11:05 AM, m.banaouas wrote:

> hi,
> Can anyone give me an example of how the PythonImport directive/ 
> code works?
> I expect to use it to pre-open an sql database connexion, keep it  
> opened and make it accessible from later requests.
> thanks for any suggestion

Presuming you are using mod_python 3.3.0b, as per documentation at:



   PythonImport module::function interpreter_name

Change names as appropriate.

Do the database initialisation within function() inside the module,  
not at global
scope because if done at global scope and it fails and raises an  
exception it
will cause import of module to fail and only way around that is an  

By doing it within the function, although it may still fail, a  
handler which requires
the database stuff can always call the function still with it doing  
nothing if it did
previously work at time of PythonImport, otherwise it would do  
delayed database
initialisation at time of first request. If it still fails, handle  
can test for that and
return an appropriate error page.

Note that the module should not be a candidate for reloading if you  
are caching
data at global scope such as database connection pool, unless you  
have specifically
coded the module to be safe in the face of automatic module reloading.

If you aren't using 3.3.0b, although you might get something going by  
having it
done at global scope, it will not be reliable and also may suffer  
because of old importer bugs.


More information about the Mod_python mailing list