Adding your own site directories cleanly and supportedly?

Peter Hansen peter at engcorp.com
Wed May 26 12:24:42 EDT 2004


Tim Bradshaw wrote:

> Ah, but.  How do I add an application-specific site directory, and do
> all the .pth file searching?  It looks like I can call
> site.addsitedir() to add a directory of my choosing, and this will all
> work.  This is all fine.
> 
> The question is: is this the right way to do this?  The documentation
 > for site doesn't describe any functions it exports, so I'm not sure if
 > I'm relying on something that might change or go away at some point.

I think the comments at the top of site.py are often considered to
be the _real_ documentation for that module.  Note especially this
one, at the end of those comments:

'''After these path manipulations, an attempt is made to import a module
    named sitecustomize, which can perform arbitrary additional
    site-specific customizations.  If this import fails with an
    ImportError exception, it is silently ignored.
'''

In the past, I've done what you describe by including a sitecustomize.py
in the application directory, and putting only these lines in it:

import site
site.addsitedir('.')

Then include one or more local .pth files and you're set.

Note that the code shown there works only if the current directory
is the directory where the application code resides, but that
was always the case in our code.  You might need something other
than '.' if that's not the case.

-Peter



More information about the Python-list mailing list