9

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 adding C:\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 in C:\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]
Running python 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 from C:\Python27\Lib, and failed due to a syntax error when it encountered a with 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.
>>>> 

4 Answers 4

2

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 $*
2
  • Printing this out revealed that I had python executable inside C:\Program Files (x86)\LilyPond\usr\bin, and renaming it fixed the issue. Thanks! Jun 7, 2012 at 20:16
  • 1
    That'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.
    – efotinis
    Jun 9, 2012 at 15:42
8

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). enter image description here

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) enter image description here

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
0
0

Posting because this thread gets top results and I don't see this answer. This simple solution is what worked for me on Windows 7. You need to look at your system variables for the Path variable.1 echo %PATH% does not seem to print them in order for me, so use the following to view and edit them:

Check your system variables by right-clicking My Computer in the Windows Menu ‣ Properties ‣ Advanced System Settings ‣ Advanced tab ‣ Environment Variables button ‣ under System Variables select Path ‣ Edit button

This will bring up a window to edit Path. If you have multiple versions of python installed, make sure the one that you want to use as default is first in the list. If you want python 2.7 to be the default, for example, that should be listed before other versions.

Mine looks like this: C:\Python27\;C:\Program Files (x86)\Python35-32\;

To confirm it worked, reboot and open cmd prompt. Then run python -V

Now it outputs Python 2.7.11 and running scripts that just rely on the python command appear to be using this version.

0

I had this problem but it happened after I installed Anaconda. It would ignore the path and always start the version of python that was installed with Anaconda. I uninstalled Anaconda and it honored the path.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Oct 18, 2022 at 16:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .