saved sys.path

Alex Martelli aleaxit at yahoo.com
Tue Nov 2 01:40:30 EST 2004


Eric S. Johansson <esj at harvee.org> wrote:
   ...
> file, command_name.path, would contain all of the search as necessary
> for a command called by the same name.  this technique would allow for a
> common platform independent method of finding application-specific 
> modules and automatically generated paths for site-specific 
> relocation/configuration.
> 
> As I write this, it occurs to me that a provisional body of code to 
> experiment with the technique would possibly serve the community well.
>   I will generate something tomorrow for public scrutiny as I am up past
> my bedtime.  Thank you for triggering the idea.

You're welcome.  Implementation should be easy, if you think that
architecture is sound.  Consider site.py, specifically the end of
function addsitedir(sitedir), which currently goes:

    names.sort()
    for name in names:
        if name[-4:] == os.extsep + "pth":
            addpackage(sitedir, name)
    if reset:
        _dirs_in_sys_path = None

It seems to me that all you need would be to make it:

    names.sort()
    for name in names:
        if name[-4:] == os.extsep + "pth" or name == specname:
            addpackage(sitedir, name)
    if reset:
        _dirs_in_sys_path = None

where specname is the application-specific path-filename you envisage.
At module-global level, where now you have:

prefixes = [sys.prefix]
sitedir = None # make sure sitedir is initialized because of later 'del'

your would add one further line going, for example:

specname = '%s.appath' % os.path.basename(sys.argv[0])


I would suggest .appath or something that stands out more than .path
which looks too much like .pth -- just IMHO.

Moreover, I think addpackage should take an optional flag:

def addpackage(sitedir, name, athead=False):

and use it when it adds to the path: instead of

        if not dircase in _dirs_in_sys_path and os.path.exists(dir):
            sys.path.append(dir)

it might go something like:

        if not dircase in _dirs_in_sys_path and os.path.exists(dir):
            if athead:
                sys.path.insert(0, dir)
            else:
                sys.path.append(dir)

Then, the call to addpackage from functon addsite should become:

        if name[-4:] == os.extsep + "pth" or name == specname:
            addpackage(sitedir, name, name == specname)

All this is untested, but we're talking about a few lines worth of patch
to site.py, nothing huge.


As to whether the whole architecture is sound -- I pass.  I'm not sure I
see why it makes sense to have a foo.appath (or foo.path as you'd want
it) that presumably gets autoedited during install to contain the
relevant dirs, rather than just autoedit foo.py, the main script, whose
purpose should, after all, be exactly to set up the environment
(sys.path first and foremost) before importing and running the real
stuff.  The current bar.pth arrangement makes sense exactly because it's
_not_ tied to apps, but to sites -- where has this site chosen to
install some backage 'bar' which must be made available for import to
all scripts running at this site.  An application, as opposed to a
package, gets to run its own code ("main script") and thus would seem
not to need such arrangements.


Alex



More information about the Python-list mailing list