Locate python home

Mike C. Fletcher mcfletch at rogers.com
Wed Feb 25 06:13:47 EST 2004


Thomas Aanensen wrote:

>>Never trust the assumption:
>>
>> >>> import os
>> >>> os.path.isfile( os.path.join(os.path.dirname( sys.executable ),
>>'python.exe' ))
>>False
>>
>>(This is from within Pythonwin, which has its executable elsewhere).
>>
>>It's a good idea to check and see if the file really does exist.  If it
>>doesn't, either raise an error to tell the user to figure out what's
>>wrong, or use the executable as given, (possibly checking/raising the
>>error *only* if the executable is named pythonw and you can't find the
>>python executable, though that would still have you running PythonWin in
>>the above example (checking for "not named python" would be safer)).
>>    
>>
>
>What about using PYTHONHOME as environment variable. Is that appropriate?
>  
>
Well, doesn't exist on my win2k machine, at least.  Using sys.executable 
with some defensive programming is probably what you really want.  Just 
*check* the assumptions and punt if there's a problem (with a useful 
error message).  99% of the time you'll be fine, and the other 1% of the 
time people are being silly anyway (such as running the script from 
Pythonwin) and just need to be knocked up-side the head with a 
cloodle-stick :) . 

"Can't find python.exe in executable's directory %r, and the running 
executable is not named python.exe, please use python.exe to run this 
script" or "Can only find pythonw.exe in directory %r, this script needs 
access to python.exe, please contact technical support to get a full 
python installation" might be suitable error messages.

Enjoy yourself,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/






More information about the Python-list mailing list