Python could be located at different paths on computers (e.g. c:\python).
How can I find this path?
Thomas 8 4026
Thomas Aanensen wrote: 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
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
> 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
Thomas Aanensen wrote: 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 (mh******@skippinet.com.au) -
see 'Help/About PythonWin' for further copyright information. import sys sys.executable
'C:\\bin\\lang\\py23\\Lib\\site-packages\\Pythonwin\\pythonwin.exe' sys.prefix
'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. import sys sys.executable
'c:\\bin\\lang\\py23\\python.exe' sys.prefix
'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. import sys sys.executable
'/usr/local/bin/python' sys.prefix
'/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/
Thomas Aanensen wrote: 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?
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)).
Anywho, have fun,
Mike
_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder http://members.rogers.com/mcfletch/
> 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?
Thomas
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/
Mike C. Fletcher wrote: Thomas Aanensen wrote: Never trust the assumption:
>>> import os >>> os.path.isfile( os.path.join(os.path.dirname( sys.executable ),
'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
--
-------------------------------------------------------------------
Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
Tel +49-241-93878-0 Fax +49-241-93878-20 eMail pe********@mplusr.de
-------------------------------------------------------------------
| ....
| 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() ....
--
Cousin Stanley
Human Being
Phoenix, Arizona This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alexander Kienzle |
last post by:
I'm new to Java programming. I'm developing a Servlet for tomcat which needs
an external configuration file. With external I mean a file (in XML format)
which
is customizable and not contained in...
|
by: mwt |
last post by:
Is there a function in python that does what "locate" does in a bash
shell?
I know I could do it by using os.popen('locate'), but I'm curious if
there's a Python "native" way to go about it....
|
by: robert |
last post by:
In a makefile I want to locate the .so for a dynamically linked
Python on Linux. (for cx_Freeze's --shared-lib-name)
e.g. by running a small script with that Python. How to?
Robert
|
by: mg |
last post by:
When make gets to the _ctypes section, I am getting the following in
my output:
building '_ctypes' extension
creating build/temp.solaris-2.10-i86pc-2.5/home/ecuser/Python-2.5.1/
Modules/_ctypes...
|
by: Stephen Cattaneo |
last post by:
Hello all,
I am attempting to execute an automated test (written in Python) via
cron. I have to check the HOSTNAME variable as part of the test, oddly
under cron the HOSTNAME environment...
|
by: Akira Kitada |
last post by:
Hi list,
I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed
to build some of the modules.
"""
Failed to find the necessary bits to build these modules:
_bsddb ...
|
by: Akira Kitada |
last post by:
Hi Marc-Andre,
Thanks for the suggestion.
I opened a ticket for this issue: http://bugs.python.org/issue4204
Now I understand the state of the multiprocessing module,
but it's too bad to see...
|
by: Martin Rubey |
last post by:
Dear all,
I'm trying to call from common lisp functions written for Sage
(www.sagemath.org), which in turn is written in python. To do so, I tried...
|
by: Philip Semanchuk |
last post by:
On Nov 9, 2008, at 7:00 PM, News123 wrote:
Look under the heading "Standard Encodings":
http://docs.python.org/library/codecs.html
Note that both the page you found (which appears to be a...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |