organizing your scripts, with plenty of re-use

Ethan Furman ethan at stoneleaf.us
Sat Oct 10 19:31:48 EDT 2009


Gabriel Genellina wrote:
> En Sat, 10 Oct 2009 05:57:08 -0300, Steven D'Aprano  
> <steve at remove-this-cybersource.com.au> escribió:
> 
>> On Fri, 09 Oct 2009 16:37:28 -0700, Buck wrote:
>>
>>> Here's a scenario. A user does a cvs checkout into some arbitrary
>>> directory and sees this:
>>>
>>> project/
>>> +-- python/
>>>     +-- animals.py
>>>     +-- mammals/
>>>         +-- horse.py
>>>         +-- otter.py
>>>     +-- reptiles/
>>>         +-- gator.py
>>>         +-- newt.py
>>>     +-- misc/
>>>         +-- lungs.py
>>>         +-- swimming.py
>>>
>>> These are all runnable scripts that "just work" with no extra effort or
>>> knowlege, both in the testing scenario above, and for normal users that
>>> run it from some central location (maybe "/tools/mycompany/bin/
>>> mammals").
>>>
>>> The frustrating thing, for me, is that all these requirements are met if
>>> you leave the scripts in jumbled into a flat directory.
>>
>>
>> I bet that's not true. I bet that they Just Work only if the user cd's
>> into the directory first. In other words, if you have all your scripts in
>> the directory /tools/mycompany/bin/scripts, this will work:
>>
>> $ cd /tools/mycompany/bin/scripts
>> $ animals.py
>>
>> but this won't:
>>
>> $ cd /home/username
>> $ /tools/mycompany/bin/scripts/animals.py
>>
>>
>> In the first case, it works because the current working directory is
>> included in the PYTHONPATH, and all the modules you need are there. In
>> the second, it doesn't because the modules aren't in either the current
>> directory or any other directory in the PYTHONPATH.
>>
>> That's my prediction.
> 
> 
> Mmm, I predict you won't have much success in your new fortune teller  
> career... :)
> You got it backwards. At least on Windows, the current directory 
> *isn't*  on the Python path, but the directory containing the script 
> *is* included.
> So both alternatives above work.
> 

Are you sure?

------
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import sys
 >>> sys.path
['',  <-- current directory?
  'C:\\WINDOWS\\system32\\python25.zip',
  'C:\\Python25\\DLLs',
  'C:\\Python25\\lib',
  'C:\\Python25\\lib\\plat-win',
  'C:\\Python25\\lib\\lib-tk',
  'C:\\Python25',
  'C:\\Python25\\lib\\site-packages',
  'C:\\Python25\\lib\\site-packages\\win32',
  'C:\\Python25\\lib\\site-packages\\win32\\lib',
  'C:\\Python25\\lib\\site-packages\\Pythonwin']

------
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

c:\temp>type print_path.py
import sys
print sys.path

c:\temp>cd \

C:\>python \temp\print_path.py
['C:\\temp',  <-- hmmm, not the current directory
  'C:\\WINDOWS\\system32\\python25.zip',
  'c:\\python25\\DLLs',
  'c:\\python25\\lib',
  'c:\\python25\\lib\\plat-win',
  'c:\\python25\\lib\\lib-tk',
  'c:\\python25',
  'c:\\python25\\lib\\site-packages',
  'c:\\python25\\lib\\site-packages\\win32',
  'c:\\python25\\lib\\site-packages\\win32\\lib',
  'c:\\python25\\lib\\site-packages\\Pythonwin']
-----

I just love learning new things!  Thanks, Gabriel!



More information about the Python-list mailing list