Python in command line runs the wrong version?
Solution 1
While you're in a Python 2.4.5 session, use this to locate the Python.exe that gets picked up:
import sys
print sys.executable
If you want to play with multiple versions, you cannot rely on %PATH%. Instead you could create separate batch files that call the version you want (make sure the batch files themselves are on the PATH though). For example, for 2.7.2 you could create a PY27.BAT that simply contains:
@C:\Python27\Python.exe $*
Solution 2
You need to update the .py extension in the registry editor. Search for the ".py" extention and look at what the (Default) key points too. It's probably in:
HKEY_CLASSES_ROOT/.py/(Default)
The "Data" of this key is probably "Python.File" (see the screenshot below).
Use this too look up a second key:
HKEY_CLASSES_ROOT/<previous_keys_data>/shell/open/command/@default
e.g.
HKEY_CLASSES_ROOT/Python.File/shell/open/command/@default
The (Default) key contains the path to the Python interpreter that will be used. Update it as required to point to the one you want to use. (see screenshot)
Background
The Python interpreter chosen on the command line only uses the PATH environment variable if you actually specify the python executable. i.e.
python myProgram.py
Without actually including "python" the command shell will attempt to find a program to open .py files (the first key above).
It will then use this key to find an appropriate program (the second key).
As @efotinis said, you can determine the interpreter being used using the simple program:
import sys
print sys.executable
Try executing this with both of the following to see the difference between using PATH to find the executable you specified and using the command interpreter to find a program to open the .py file you specified.
python myProgram.py
myProgram.py
Related videos on Youtube
Michael0x2a
As I'm sure you can probably guess from looking at my posting history, I'm primarily interested in computer science and have been programming in some capacity for about the past decade or so. I sporadically answer questions on StackOverflow and lurk in the other StackExchange websites. I've also become interested in CS education semi-recently, so I do some stuff there too. Background: I'm currently a software developer at Dropbox. I also contribute pretty heavily to mypy, a static type checker to Python, and so as of late have been focusing mostly on answering questions related to static typing in Python.
Updated on September 18, 2022Comments
-
Michael0x2a over 1 year
I have several versions of Python installed on a Windows 7 computer.
I want to run Python 2.7 by default, but for whatever reason, typing
python
in the command line runs Python version 2.4.5. I've tried addingC:\Python27
to my system path variable as per this question, and manually combed my path variable it to make sure Python 2.4.5 wasn't tossed in there by mistake, but that didn't fix the issue. I have to type inC:\Python27\python.exe
every time I want to access the correct version of python I want.What other places can I check? How can I make the command line use the correct version of python?
I also found this but it's not for windows.
[EDIT]
My path (separated by semicolons):C:\Program Files\Common Files\Microsoft Shared\Windows Live; C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live; C:\Windows\system32; C:\Windows; C:\Windows\System32\Wbem; C:\Windows\System32\WindowsPowerShell\v1.0\; C:\Program Files\Dell\DW WLAN Card\Driver; C:\Program Files (x86)\Common Files\Roxio Shared\DLLShared\; C:\Program Files (x86)\Windows Live\Shared; c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\; c:\Program Files\Microsoft SQL Server\100\Tools\Binn\; c:\Program Files\Microsoft SQL Server\100\DTS\Binn\; C:\Program Files\TortoiseGit\bin; C:\Program Files\Java\jdk1.6.0_26\bin; C:\Program Files\Java\jdk1.6.0_21 ; C:\Program Files\IVI Foundation\VISA\Win64\Bin\; C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\; C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin; C:\Program Files\WPIJavaCV\OpenCV_2.2.0\bin; C:\Program Files (x86)\LilyPond\usr\bin; C:\Program Files\TortoiseSVN\bin; C:\Program Files (x86)\doxygen\bin; C:\Program Files (x86)\Graphviz 2.28\bin; C:\Users\Michael\bin\Misc\cppcheck\; C:\Program Files (x86)\Git\cmd; C:\Python27\python.exe; C:\Ruby192\bin; C:\Users\Michael\AppData\Roaming\cabal\bin; C:\Python27\;
[EDIT 2]
Runningpython
spews this out:'import site' failed; used -v for traceback Python 2.4.5 (#1, Jul 22 2011, 02:01:04) [GCC 4.1.1] on mingw32 Type "help", "copyright", "credits" or "license" for more information. >>>
...and running
python --version
(as suggested below) seems to be an unrecognized option.(I also tried running
python -v
, and it appears that Python 2.4 is trying to import libraries fromC:\Python27\Lib
, and failed due to a syntax error when it encountered awith
statement, which was added in later version, I think)Also, I'm not sure if it's significant or not, but the above python version says something about GCC and mingw32, while running
C:\python27\python.exe
shows this:Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>>
-
Michael0x2a almost 12 yearsPrinting this out revealed that I had python executable inside
C:\Program Files (x86)\LilyPond\usr\bin
, and renaming it fixed the issue. Thanks! -
efotinis almost 12 yearsThat's great. You should also try LilyPond after the change, because it may be incompatible with the newer Python version. All in all, I would say that this is LilyPond's fault. Adding PATH entries (especially for GUI programs) always ends in pain and shouldn't be done at the drop of a hat. Unfortunately, GNU/*nix programs are often guilty of this.