Using “Edit with IDLE” with a Python 2.6.5 script

2019-04-12 21:03发布

问题:

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?

回答1:

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.



回答2:

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.