Locate python home

T

Thomas Aanensen

Python could be located at different paths on computers (e.g. c:\python).
How can I find this path?

Thomas
 
P

Peter Maas

Thomas said:
Python could be located at different paths on computers (e.g. c:\python).
How can I find this path?

import sys
print sys.executable

Mit freundlichen Gruessen,

Peter Maas
 
M

Mike C. Fletcher

Thomas said:
Python could be located at different paths on computers (e.g. c:\python).
How can I find this path?
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 ([email protected]) -
see 'Help/About PythonWin' for further copyright information.'C:\\bin\\lang\\py23'

V:\cinemon>python
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.'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.'/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
http://members.rogers.com/mcfletch/
 
T

Thomas Aanensen

import sys
print sys.executable

Thanks, but there is still one problem:

This leads to the path C:\python\Python23\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
 
M

Mike C. Fletcher

Thomas said:
Thanks, but there is still one problem:

This leads to the path C:\python\Python23\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?
Never trust the assumption:
'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
http://members.rogers.com/mcfletch/
 
T

Thomas Aanensen

Never trust the assumption:
'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?

Thomas
 
M

Mike C. Fletcher

Thomas said:
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/
 
P

Peter Maas

Mike said:
Thomas Aanensen wrote:
Never trust the assumption:

'python.exe' ))
False

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
 
C

Cousin Stanley

| ....
| just need to be knocked up-side the head
| with a cloodle-stick
| ....

I always try to keep one handy,
but too often MUST apply self.cloodle_stick() ....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top