mod_wsgi/python sys.path.exend problems
Question
I'm working on a mod_wsgi script.. at the beginning is:
sys.path.extend(map(os.path.abspath, ['/media/server/www/webroot/']))
But I've noticed, that every time I update the script the sys.path var keeps growing with duplicates of this extension:
['/usr/lib64/python25.zip'
'/usr/lib64/python2.5'
'/usr/lib64/python2.5/plat-linux2'
'/usr/lib64/python2.5/lib-tk'
'/usr/lib64/python2.5/lib-dynload'
'/usr/lib64/python2.5/site-packages'
'/usr/lib64/python2.5/site-packages/Numeric'
'/usr/lib64/python2.5/site-packages/gtk-2.0'
'/usr/lib64/python2.5/site-packages/scim-0.1'
'/usr/lib/python2.5/site-packages'
'/media/server/www/webroot'
'/media/server/www/webroot'
'/media/server/www/webroot'
'/media/server/www/webroot']
It resets every time I restart apache.. is there any way to make sure this doesn't happen? I want the module path to be loaded only once..
Solution
No need to worry about checking or using abspath yourself. Use the ‘site’ module's built-in addsitedir function. It will take care of these issues and others (eg. pth files) automatically:
import site
site.addsitedir('/media/server/www/webroot/')
(This function is only documented in Python 2.6, but it has pretty much always existed.)
OTHER TIPS
One fairly simple way to do this is to check to see if the path has already been extended before extending it::
path_extension = map(os.path.abspath,['/media/server/www/webroot/'])
if path_extension[0] not in sys.path:
sys.path.extend(path_extension)
This has the disadvantage, however, of always scanning through most of sys.path
when checking to see if it's been extended. A faster, though more complex, version is below::
path_extension = map(os.path.abspath,['/media/server/www/webroot/'])
if path_extension[-1] not in reversed(sys.path):
sys.path.extend(path_extension)
A better solution, however, is probably to either add the path extensions to your PYTHONPATH
environment variable or put a .pth
file into your site-packages
directory:
The mod_wsgi documentation on code reloading covers this.