vendredi 30 août 2013

Edit Python files with IDLE from WinSCP

Pour une version française de l'article, cliquez sur ce lien : http://antoine-agthe.blogspot.com/2013/08/editer-des-fichiers-python-avec-idle.html


What is WinSCP?


WinSCP for Windows is a simple and powerful tool that lets you work with your server throught SSH while taking benefits of your favorite edition tools, on your favorite Operating System (I stay neutral, never said that my case, or not)


What is Python (briefly)?


Python is a programming language, quite effective, and that simple, for heavy development as for shell script.
It is a good way for whom wants to get started with the wonderful world of programming.


Context


As I was on a project that needed a Facebook connection server-side for statistic processing called once a week (CRON), I chose to use Python.

The server was an Amazon EC2 instance configured with Ubuntu LTS, and my working environment was a PC with Windows 7. I needed a simple solution to edit script files from my server directly on my computer.

I use WinSCP for years now because it makes it easy, by downloading a copy of the edited file into a local temporary file, then listening its updates, in order to upload it automatically when it's modified.

However, the default text editor of WinSCP is not enough convenient for source code editing. And so for Python files. Even if Notepad++ is my favorite editor for many file formats, it does not satisfy me as much as the default Python file editor, IDLE, provided by the Python installer.


Problem


Make IDLE be the default editor of Python files on WinSCP is not as easy as with Notepad++, because IDLE is not an executable, properly speaking.
IDLE is a Python script, interpreted by Python itself, that provides a GUI for python file editing.
So that's not a simple "path to EXE" that you have to set up in WinSCP, but a command line.


Solution


I found a way to do it thanks to the Windows registry.
Don't panic, we won't make anything in that registry, but get a string value.

On Windows, when you wanna edit a Python file, you open the context menu of the file (right click), then select Edit with IDLE.
If you don't know, you'll learn that these contextual functions are defined in the Windows registry.

To find the proper command line, I just called regedit.exe (Windows key + R, then type regedit), then searched (Ctrl+F, or Edit->Search) the following expression : Edit with IDLE.

I got a registry entry HKEY_CLASSES_ROOT\Python.File\shell\Edit with IDLE, that contained a key called command, with the following value:

"C:\Python27\pythonw.exe" "C:\Python27\Lib\idlelib\idle.pyw" -e "%1"

So in order to use IDLE with WinSCP for Python files, you just need to open the Preferences window then click "Editors", and add a new configuration for files with .py extension, select External editor, then type :

C:\Python27\pythonw.exe C:\Python27\Lib\idlelib\idle.pyw -e "!.!"


1 commentaire:

  1. Thank you! Works beautifully.

    Took me a minute to figure out that I had to move the *.py item up in the list of editors from Preferences -> Editors. Otherwise it uses the "Internal Editor" before it reaches the .py option.

    RépondreSupprimer