13

I have two python versions: Python 2.5 and Python 2.7. When I'm setting up django, I want to use Python 2.7, but django is using Python 2.5 somehow.

How can I make sure that django use Python 2.7? I'm using Windows XP

KenHBS
  • 6,756
  • 6
  • 37
  • 52
zjm1126
  • 34,604
  • 53
  • 121
  • 166
  • Possible duplicate of [Changing python interpreter windows](http://stackoverflow.com/questions/1053794/changing-python-interpreter-windows) – ivan_pozdeev Mar 14 '16 at 17:49

7 Answers7

21

Changing your PATH will help, if you always call python directly, rather than relying on file association.

For example: "python foo.py" will run the 'foo' script with whichever python is first on your PATH.

However, if you just run "foo.py", the handler associated in the registry, for this file extension, will be the first one called.

In your case, it sounds like that would be python 2.5. Have a look by opening regedit, and checking the values of:

HKEY_CLASSES_ROOT\Python.File\shell\open\command

The (default) value listed will probably be something like "C:\Python25\python.exe" "%1" %*

A quick (and dirty?) fix for you would be to change these values to the python version you actually want.

A better fix would be to do something like what's outlined in this feature request:

http://bugs.python.org/issue4485

Since then, as long as you had admin rights, you could switch as you needed by pointing assoc at the version you want quickly.

otherchirps
  • 1,836
  • 1
  • 19
  • 26
  • 6
    BTW Quick way to find out handler for python file extension: `ftype|find /I "python"` – Alex Markov Apr 13 '12 at 08:48
  • this answer provides more details on finding what is associated with .py http://stackoverflow.com/a/5088548/264607 and shitalshah answer is more current: http://stackoverflow.com/a/24277570/264607 – BlackICE Dec 19 '16 at 11:52
12

Make two simple .cmd files:

python25.cmd:

@echo off
set PYTHONHOME=c:\python25
set PATH=%PATH%;c:\python25

python27.cmd:

@echo off
set PYTHONHOME=c:\python27
set PATH=%PATH%;c:\python27

Now you can switch between Python 2.5 and 2.7. :)

Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
9

Change your PATH system environment variable to point to the version of Python you want to use.

John Percival Hackworth
  • 11,395
  • 2
  • 29
  • 38
5

Python installer does no change to PATH environment variable under windows. When typing something like python setup.py windows first looks for python in PATH, then in current user registry hive

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\App Paths\Python.exe

and then in local machine registry hive

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\App Paths\Python.exe
nogus
  • 1,500
  • 1
  • 11
  • 6
5

Now that Python 3.3 is released it is easiest to use the py.exe utility described here: http://www.python.org/dev/peps/pep-0397/

It allows you to specify a Python version in your script file using a UNIX style directive. There are also command line and environment variable options for controlling which version of Python is run.

The easiest way to get this utility is to install Python 3.3 or later.

Gerald
  • 609
  • 5
  • 13
4

If you want to switch between Python 2.x and Python 3.x then easiest way is to use Python Launcher which is included since 3.3 version. This is basically py.exe in Windows folder. To start Python 3.x command prompt, just type

py -3

To execute script with Python 3.x, use

py -3 script.py

If you don't specify -3 then 2.x version is used by default. You can also make this explicit by using -2.7 switch.

py -2.7 script.py

Finally, you can now embed the version number to use in .script file itself. This works because after Python 3.3+ is installed, it associated py.exe with .py files.

#! python3
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
1

Change the registry key at

HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
jnnnnn
  • 3,889
  • 32
  • 37