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..

Accepted Answer

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

Written by bobince
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki