saved sys.path

Eric S. Johansson esj at harvee.org
Tue Nov 2 18:44:06 EST 2004


Alex Martelli wrote:
...bunch of good stuff

here's my working model.  It's barely tested and I'll experiment with it 
in some code of the next couple of days.

its usage is relatively simple.  Stick this bit of code in 
site-packages.  I call it application_path.py because it's easy to 
dictate.  Then in a piece of code such as xyzzy.py, you start it was 
something like:

#!/usr/bin/python

import application_path
application_path.set_path()
import sys
print sys.path

and create xyzzy.pathlist in site-packages.  Fill it with a list of 
directories that exist.  If directories don't exist, they will not be 
added to the search path.  And run the executable.  The print statement 
will show you modified path.  In this example, the default is to prepend 
paths because in my world, I want my changes to be found first just in 
case there is a naming conflict or I am overriding a system module.

the first argument to set_path is the name.  specifying the name is 
useful obviously because you may have multiple executables that use the 
same module set and this allows the application set to use a single path 
file.  The second is at_head which if true (default) places the paths at 
the beginning of sys.path.  If false, obviously they are appended to the 
end.

as for the code style, it's mostly influenced by what is easy to do 
using speech recognition and Emacs.  There have been a couple of 
projects to try and make programming by voice easier but they have run 
out of steam or run over by complexity.  Maybe both.


#!/usr/bin/python

#application_path is a module an application can use to add
#application-specific paths to an application's awareness

import sys
import os.path
import re

def set_path(name = None, at_head = True):
     """expand the site path from the file matching the given name or
     the executable name.  Always append to the head of the search path
     unless at_head == False"""

     working_name = name
     if name == None:
         # strip off suffix and/or leading path
         working_name = sys.argv[0]

     # print "proto working name %s"% working_name
     dot_index =  working_name.rfind(".")
     if dot_index != -1:
         working_name =  working_name[0:dot_index]
     working_name = os.path.basename(working_name)

     # convert name to path list
     path_list = get_paths(working_name)

     if at_head:
         path_list.extend(sys.path)
         sys.path=path_list

     else:
         sys.path.extend(path_list)

     print sys.path
     return working_name


def get_paths (name):
     """based on a given name (which should be fully cleaned up to be
     just a name and not a path with extension) get a path file and  """
     file_path = "".join([sys.prefix,
                          "/lib/python",
                          sys.version[:3],
                          "/site-packages/",
                          name,
                          ".pathlist",
                          ]
                         )
     try:
         #print "file_path %s" % file_path
         handle = open(file_path, 'r')

         path_list = []
         for line in handle.readlines():
             line = line.strip()

             #ignore comments and blank lines
             ignore = re.search(r'^#|^\s+$', line)
             if ignore : continue

             # does path exist?
             if os.path.isdir(line):

                 # build a list of paths
                 path_list.append(line.strip())

         handle.close()

         return path_list

     except IOError:
         #print "exception: file_path %s" % file_path
         return []


if __name__ == '__main__':

     set_path()
     print
     print

     set_path("testing")

     print
     print

     set_path("testing", False)





More information about the Python-list mailing list