organizing your scripts, with plenty of re-use

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Oct 6 11:35:09 CEST 2009


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

> On Mon, 05 Oct 2009 13:46:09 -0700, Buck <workitharder at gmail.com> 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 mypathmunge.py in  
> site-packages that goes:
>
> # mypathmunge.py
> 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).

[1] http://www.python.org/dev/peps/pep-0370/

-- 
Gabriel Genellina




More information about the Python-list mailing list