2

I have installed both Python 2.7.1 and 2.6.5 versions on Windows. I have added only the path of Python 2.6.5 in the Environment Variables. Now, I want to run a Python script x.py using Python 2.6.5. I know how to do it using the cmd but It would be more convenient to just open it with IDLE and run inside it using the Run Module option. This is supposedly done by right-clicking over the script, and then going to Edit with IDLE option, but this opens and runs the script using Python 2.7.1. Is there a way to open and run it with Python 2.6.5?

multigoodverse
  • 7,638
  • 19
  • 64
  • 106
  • 2
    This is held in Held in `HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE\command`. ++ for the question, I had never noticed that changing `FTYPE Python.File` did not alter the "Edit with IDLE" (obvious now you mention it though). See also http://stackoverflow.com/questions/5168374/edit-with-idle-python-gui-context-menu-windows-7 – cdarke Mar 11 '13 at 16:16

2 Answers2

4

The standard command in the registry for Edit with IDLE is the following:

"C:\Program Files\Python33\pythonw.exe" "C:\Program Files\Python33\Lib\idlelib\idle.pyw" -e "%1"

Now, as you can see, the path is hardcoded into it, so it just cannot use a different Python interpreter like that—at least per default.

However, PEP 397 introduced a new Python launcher for Python making it possible to launch different versions of Python based on the shebang line. So a file starting with #!/usr/bin/env python2 will launch the current Python 2 interpreter, while #!/usr/bin/env python3 will use Python 3.

Using that information, you can launch IDLE for a given Python version dynamically. For example this would edit the file using the launcher’s default Python version:

 C:\Windows\pyw.exe -m idlelib.idle -e "%1"

This would force the use of Python 3

 C:\Windows\pyw.exe -3 -m idlelib.idle -e "%1"

And this would force the use of Python 2:

 C:\Windows\pyw.exe -2 -m idlelib.idle -e "%1"

So what you would need to do is write a script that basically checks which Python version a file wants to be executed with, i.e. by parsing the shebang line manually (sadly the launcher does not give you this information without actually launching the script—yet. I might start a pull request to get such a feature into the core). Then you would run either the Python 2 or Python 3 IDLE using the command above and done.

You would just need to change the Edit with IDLE command to execute your script then and it would work.

A very simple alternative would be to just add another registry key which launches the Python 2 IDLE. So you would have Edit with IDLE and Edit with IDLE (Py2) or something.

To do that, just put the following inside a .reg file and execute it:

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)]
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\Edit with IDLE (Py2)\command]
@="C:\\Windows\\pyw.exe -2 -m idlelib.idle -e \"%1\""

edit: I just noticed that I wrote this mainly about Python 2 vs. Python 3; the launcher ships with Python 2.7 I think, so this will work just the same. You just need to adjust the launcher’s version specificiers to -2.6 and -2.7 or whatever you want.

poke
  • 369,085
  • 72
  • 557
  • 602
  • What is the use of "-m" flag? – Deamonpog Mar 14 '15 at 03:57
  • @Deamonpog `python -m something` in general launches the module `something`. This works with all executables of Python, including the Windows launcher. So basically `pyw.exe -m idlelib.idle` says to launch the windowed Python executable and load the `idlelib.idle` module (which will effectively launch IDLE). – poke Mar 14 '15 at 19:05
  • I quite didn't understand the need of using `-m` flag for this, what happens if I don't give `-m` flag? (Your solution worked for me but I couldn't find any man page or help text describing `m` flag. I tried removing `-m` and running the command and nothing happened.) – Deamonpog Mar 15 '15 at 03:53
  • @Deamonpog Running `python something` will try to run the script file `something` (usually it would be `something.py`), so if you were to run `python idlelib.idle` there would have to be a Python script called `idlelib.idle` in the current directory that would then be executed. But using `-m` instead, Python will look through its module library and run that module instead. – poke Mar 15 '15 at 21:22
1

You can do this with some registry hacks to make IDLE 2.6 your default (rather than 2.7), but that's not really what you want, I think, since then you'd have to reverse the process when you want to test something in 2.7. Unless someone else knows some other way to integrate different IDLE installs into the shell, here are a couple better options:

  1. Open IDLE 2.6 first, and just use the Open File dialog from the GUI.

  2. Use a different IDE that actually supports this functionality. Eclipse with PyDev will let you switch interpreters between runs, or save configurations with different interpreters, and so on.

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • What would happen if I uninstall Python 2.7? Would the "Edit with IDLE" option work with Python 2.6 or it would not even show up? – multigoodverse Mar 11 '13 at 16:22