This leads to the path C:\python\Pytho n23\pythonw.exe . In my application I
cannot use pythonw.exe, I need python.exe (console reasons). Is there some
way to get this instead?
If not, would it be ok to assume that python.exe will always reside in the
same directory as pythonw.exe?
Thomas Aanensen wrote:
[color=blue]
>Python could be located at different paths on computers (e.g. c:\python).
>How can I find this path?
>
>[/color]
PythonWin 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32.
Portions Copyright 1994-2001 Mark Hammond (mhammond@skipp inet.com.au) -
see 'Help/About PythonWin' for further copyright information.[color=blue][color=green][color=darkred]
>>> import sys
>>> sys.executable[/color][/color][/color]
'C:\\bin\\lang\ \py23\\Lib\\sit e-packages\\Pytho nwin\\pythonwin .exe'[color=blue][color=green][color=darkred]
>>> sys.prefix[/color][/color][/color]
'C:\\bin\\lang\ \py23'
V:\cinemon>pyth on
Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
>>> import sys
>>> sys.executable[/color][/color][/color]
'c:\\bin\\lang\ \py23\\python.e xe'[color=blue][color=green][color=darkred]
>>> sys.prefix[/color][/color][/color]
'C:\\bin\\lang\ \py23'
Python 2.3.3 (#1, Jan 29 2004, 21:46:45)
[GCC 3.2.2 [FreeBSD] 20030205 (release)] on freebsd5
Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
>>> import sys
>>> sys.executable[/color][/color][/color]
'/usr/local/bin/python'[color=blue][color=green][color=darkred]
>>> sys.prefix[/color][/color][/color]
'/usr/local'
That is, sys.executable will give you the name of the executable which
is currently running. Py2exe, PythonWin, etceteras can all wind up
making it say things you might not consider to be Python (they're not
python.exe), but it's the best approximation to "where's Python" for
most needs of the information. sys.prefix/sys.exec_prefix is going to
need platform-specific munging to make any sort of sense for most values
of "where's Python". See documentation on the sys module for more details.
HTH,
Mike
_______________ _______________ _________
Mike C. Fletcher
Designer, VR Plumber, Coder
Thomas Aanensen wrote:
[color=blue][color=green]
>>import sys
>>print sys.executable
>>
>>[/color]
>
>Thanks, but there is still one problem:
>
>This leads to the path C:\python\Pytho n23\pythonw.exe . In my application I
>cannot use pythonw.exe, I need python.exe (console reasons). Is there some
>way to get this instead?
>
>If not, would it be ok to assume that python.exe will always reside in the
>same directory as pythonw.exe?
>
>[/color]
Never trust the assumption:
[color=blue][color=green][color=darkred]
>>> import os
>>> os.path.isfile( os.path.join(os .path.dirname( sys.executable ),[/color][/color][/color]
'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)).
Anywho, have fun,
Mike
_______________ _______________ _________
Mike C. Fletcher
Designer, VR Plumber, Coder
> Never trust the assumption:[color=blue]
>[color=green][color=darkred]
> >>> import os
> >>> os.path.isfile( os.path.join(os .path.dirname( sys.executable ),[/color][/color]
> '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)).[/color]
What about using PYTHONHOME as environment variable. Is that appropriate?
Thomas Aanensen wrote:
[color=blue][color=green]
>>Never trust the assumption:
>>[color=darkred]
>> >>> import os
>> >>> os.path.isfile( os.path.join(os .path.dirname( sys.executable ),[/color]
>>'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)).
>>
>>[/color]
>
>What about using PYTHONHOME as environment variable. Is that appropriate?
>
>[/color]
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
Mike C. Fletcher wrote:[color=blue]
> Thomas Aanensen wrote:
> Never trust the assumption:
>[color=green][color=darkred]
> >>> import os
> >>> os.path.isfile( os.path.join(os .path.dirname( sys.executable ),[/color][/color]
> 'python.exe' ))
> False[/color]
If Thomas wants to find the console version he should start a
console script: python checkpath.py (assuming python is in the
PATH). sys.executable delivers always the executable that executes
the script. If python is not in the PATH there's no safe way to
find python.exe as far as I know.
Mit freundlichen Gruessen,
Peter Maas
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail peter.maas@mplu sr.de
-------------------------------------------------------------------
Comment