Thursday, August 6, 2009

Notepad++ and Python

I know, I know, I'm using Windows. But it's just temporary, I just felt like playing some video games. Anyway, Notepad++ is awesome, I must say. And I wanted to use it for Python developing (I hate that IDLE Python comes with). To set up Notepad++ for Python developing I had to:

Create a batch script that would run a python script and then wait for a key (so that the terminal (or, command line) doesn't disappear);

Configure a shortcut in Notepad++ to run that script with the current file as parameter.

First things first: the batch script. Pretty basic, I just looked at a couple of Wikipedia articles and another one about using arguments in batch scripts:
@ECHOOFF
C:\Python26\python.exe "%1"echo.
PAUSE@ECHOON
Then, in Notepad++, I went to Run → Run..., type in C:\Python26\python.bat "$(FULL_CURRENT_PATH)", then Save... and assign a shortcut. Press that shortcut now with a python file opened and boom goes the dynamite. Enjoy :)

I setup notepad++ using these settings long ago. At one point I lost my configuration information, and forgot how I'd set things up to pause when running python scripts. I'm glad I rediscovered this page as I'm up and running again. Don't ever let this page disappear. THANKS!

1 - Save this as pt.bat in the directory where python is installed. In my case that is C:\Program Files\python32\

2 - Make sure the python directory is in the PATH environmental variable. This is advised by Python installation. Directions at end.

3 - in Notepad++ click Run. Type

pt "$(FULL_CURRENT_PATH)"

into the box and click Save...

3 - Enter a name like PyRun and select a shortcut key. In my case I entered PyRun and Ctrl Shift B. Then click OK. Make sure you use a shortcut that is not used by some editing command. You can see the shortcuts by clicking Settings>Shortcut Mapper... in Notepad++.

4 - Now, if you have a file open in Notepad++ with the extension .py (ex. allnames.py) you can run that file in a command window by pressing your shortcut (Ctrl Shift B in my case). Alternatively, if you click the Run command you will see the PyRun command at the bottom of the run list. Select it and click.

What's going on here? - When you click the shortcut, the name of the file you are editing is substituted for "$(FULL_CURRENT_PATH)", and the Windows command line is givenpt "$(FULL_CURRENT_PATH)"- The directories on PATH are searched for an executable file named pt (in our case pt.bat in the python directory)- When pt.bat is found, "$(FULL_CURRENT_PATH)" is substituted for %1. If Python is correctly installed on your machine, .py files will already be associated running Python.- pt.bat opens a command line window and runs the commands in the .bat file.- the filename "$(FULL_CURRENT_PATH)" has an extension .py so Win runs python.exe and hands it the file "$(FULL_CURRENT_PATH)"- When python finishes, control reverts back to the command line window where pt.bat has a PAUSE statement. Thus you can see the results of the Python execution.

The environmental variables can be found by right clicking the My Computer icon on the desktop, selecting properties>advanced>Environment Variables. Go down the list till you see PATH. All the directories on this path are searched for executable file types, such as .exe .bat .cmd etc.

Thx... Exactly what I was looking for.I am pretty new to all this, but I was wondering if it is possible to set up the run command, so it automatically saves the current file before running python? It would make the work a bit more smooth, instead of having to save each time before you run Python.

I just started with Pythn 2 days back. I am using Windows as my OS. I have installed ActiveStateActivePython, Python2.7.3, Notepad++I tried a few programs n it works pretty fine, yet I am not sure from where are things really working..I dont think I need all the installations rt? Which shall I uninstall?Also I wanted to know the reason behind "1%" in the py.bat Awaiting reply

Hi Rubina. The %1 is a macro that gets replaced with the first argument that gets passed to the script. Thus, when Notepad++ executes the script, it will pass the current file as the first argument and the batch script will call Python and tell it to run that file.

An idea if this isn't working for you: remove the quotation marks around the "%1" in the batch file. Since you're already including quotation marks around the argument in notepad++, this extra pair isn't necessary, and in fact might break things if your filename has spaces.

You do need the quotation marks around the $(FULL_CURRENT_PATH) in notepad++ since otherwise a filename with spaces will get passed as multiple arguments.

I know this was posted a long time ago, but for the record you can do it without the .bat file, in one go. In the run menu, instead of pointing to the .bat file point directly to the python .exe and use the "-i" flag to keep it open. So instead of running:C:\Python26\python.bat "$(FULL_CURRENT_PATH)"you'd haveC:\Python26\python.exe -i "$(FULL_CURRENT_PATH)"and no need for a .bat file.