organizing your scripts, with plenty of re-use

Gabriel Genellina gagsl-py2 at
Tue Oct 6 11:35:09 CEST 2009

En Mon, 05 Oct 2009 18:15:15 -0300, Rami Chowdhury  
<rami.chowdhury at> escribió:

> On Mon, 05 Oct 2009 13:46:09 -0700, Buck <workitharder at> wrote:
>> Thanks. I think we're getting closer to the core of this.
>> To restate my problem more simply:
>> My core goal is to have my scripts in some sort of organization better
>> than a single directory, and still have plenty of re-use between them.
>> The only way I can see to implement this is to have 10+ lines of
>> unintelligible hard-coded boilerplate in every runnable script.
>> That doesn't seem reasonable or pythonic.
> If there's a standard directory you expect them to be dropped into by  
> your users (e.g. $HOME/scripts) then surely you could do something like:
> 	import mypathmunge
> at the top of every script, and then have a in  
> site-packages that goes:
> #
> import sys, os
> sys.path.insert(0, os.path.join(os.getenv('HOME'), 'scripts'))

Since Python 2.6 and up, you don't even need that. There exists a per-user  
site directory (~/.local/lib/python2.6/site-packages on Linux; under  
%APPDATA% on Windows) that is prepended to sys.path automatically; .pth  
files found there are honored too (see PEP 370 [1]).

So there is no need to set the PYTHONPATH variable, nor alter the standard  
site-packages directory, nor play tricks with sys.path - just install the  
modules/packages inside the user site directory (or any other directory  
named in a .pth file found there).


Gabriel Genellina

More information about the Python-list mailing list