[mod_python] PythonImport: Can someone please draw a diagram for this idiot?

Jorey Bump list at joreybump.com
Wed Sep 14 22:24:17 EDT 2005


Jim Gallacher wrote:
> Jorey Bump wrote:
>>
>> Yes, it does. For a simple demo, put a module in the default module 
>> search path, such as a directory shown in sys.path when running python 
>> from the command line. PythonPath directives in httpd.conf will not 
>> have taken effect when PythonImport runs (I used 
>> /usr/lib/python2.4/site-packages/ for my test).
>>
>> # mpglobal.py
>>
>> import time
>>
>> f = open('tmp/atest', 'w')
>> f.write(str(time.time()))
>> f.close()
>>
>> foo = 'testing'
>>
>>
>> Then in httpd.conf (using your own interpreter name):
>>
>> PythonImport mpglobal www.example.com
>>
>> On Linux, restarting apache creates and/or updates /tmp/atest, 
>> verifying that it works.
>>
>> But, like the OP, I can't figure out how to access foo (or 
>> mpglobal.foo) in my published module.
> 
> 
> I don't know if this is the correct way, but I tried it and it works:
> 
> import sys
> mpglobal = sys.modules['mpglobal']
> 
> def index(req):
>     req.write('Hello world\n')
>     req.write(mpglobal.foo)

That's very interesting, because due to module caching, the results are 
the same if you simply do this:

import mpglobal

In the published module, when mpglobal is imported, /tmp/atest isn't 
updated and mpglobal.foo is available, indicating that one could 
initialize a db connection or variables using PythonImport, yet ensure 
the module will still work if sys.modules['mpglobal'] isn't available.

As for how robust this is for maintaining a persistent db connection, 
that's another question.




More information about the Mod_python mailing list