Permanently Adding A Directory to Pythonpath

Mike C. Fletcher mcfletch at home.com
Wed May 30 17:22:44 EDT 2001


Hmm, editing sitecustomize really doesn't solve the original problem unless
you're encouraging people to rewrite the code module programmatically (which
isn't normally a good idea).  What's missing is an API that predictably,
_reversibly_, hygienically, yada, yada adds an item to a data structure used
for initialisation.  That is:

	file = somehowfind( sitecustomize.py)
	data = open(file).read() + "\n" + MYCUSTOMPATHAPPENDCODEORWHATEVER
	open( file, 'w').write( data )

Just isn't a great API for customisation, especially when you try to reverse
the process and remove those directories.

[STARTUP]
;Adds items to sys.path
PATH=yada/there;yoda/them;luke/use/force;that/way/darkness/lies;
;Executes these items
SCRIPT=site_special_stuff.py

[INTERACTIVE]
;Adds items to sys.path
PATH=yada/there/special;
;Executes these items
SCRIPT=interactive_special_stuff.py;interactive.py

[INSTALLEDPACKAGES]
;lists IDs of installed packages
Numeric=19.0.2;c:/bin/lang/python20/Numeric
mcf=1.0a;p:/mcf
simpleparse=1.0b;p:/simpleparse
PyOpenGL=1.5.6;c:/bin/lang/python20/PyOpenGL

With automated handling of the files using ConfigParser (or a more robust
version thereof) so that the API becomes something like:

	mapping = sys.getConfigurationMapping().get( "STARTUP")
	mapping[ "PATH" ].append( "lies/that/way/darkness" )
	# assume the following removes all equal instances...
	mapping[ "PATH" ].remove( "luke/use/force" )

	mapping = sys.getConfigurationMapping().get( "INSTALLEDPACKAGES")
	mapping[ myProgramSpecialName ] = [ myProgramVersion, myProgramLocation ]

or maybe:
	mapping[ myProgramGUID ] = [ myProgramName, myProgramVersion,
myProgramLocation ]


With the various wrapped objects taking care of the storage to disk on
deletion of the object, the parsing of the data structures, and the later
linearisation.  The idea being that on, e.g. Win32, you could use the
registry APIs to do this "natively" without changing the api.  Similarly,
stand-alone interpreters could decide not to use the registry on Win32, and
instead use an INI file so that they are not related to the main system.

Code as configuration is acceptable if you have huge numbers of dynamic
values with arbitrarily complex processing requirements for determining any
given item, and you can require human intervention for every change, but
these are bog-simple configuration items, a small number of lists of strings
that are constantly being manipulated.

Just a thought,
Mike




-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Alex Martelli
Sent: May 29, 2001 17:36
To: python-list at python.org
Subject: Re: Permanently Adding A Directory to Pythonpath


"Bengt Richter" <bokr at accessone.com> wrote in message
news:3b13f162.640694710 at wa.news.verio.net...
    ...
> Well, sys.path already has methods by its nature as a [] thingy,
> so why not just use those? What you'd need is a module that
> got imported by the main on startup.  Being a pythonewbie,
> I'm not sure something like that doesn't already exist ;-)

Yes, it's site.py in the lib directory.  In turn, it imports
sitecustomize, so you can do your tweaks there.


> E.g., what if starting the interpreter would automatically
> import, e.g., config.py it it existed in the PYTHONHOME
> directory? That would seem pretty extendable and flexible.
>
> Maybe whatever is reading .PTH files is it? Is it defined
> in a user-modifiable place?

I wouldn't suggest changing site.py (the one that's
reading the .pth files), but tweaks in sitecustomize.py
should work just as well.


Alex



--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list