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: @ECHO OFF C:\Python26\python.exe "%1" echo. PAUSE @ECHO ON 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 :)

29 comments:

  1. This is exactly what I am looking for.

    ReplyDelete
  2. Thanks! Btw, I had to remove the quotation marks in the batch file, but I'm a n00b so I'm not sure why. Using Python 3.1, Notepad 5.6.4, XP.

    ReplyDelete
  3. Awesome. Thanks!

    ReplyDelete
  4. Thank you so much been looking for this!

    ReplyDelete
  5. Works like a charm. Much appreciated.

    ReplyDelete
  6. This is great, thanks! exactly what i needed!

    ReplyDelete
  7. Still helping! Thank you!

    ReplyDelete
  8. Thank you very much, simple, effective and efficient. No more IDLE!

    ReplyDelete
  9. Awesome! Thanks :-)

    ReplyDelete
  10. Ditto! thank you!

    ReplyDelete
  11. 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!

    ReplyDelete
  12. Brilliant, thank you x 100

    ReplyDelete
  13. The changing to a bat file to prevent the closing was what I was missing - thanks for the post!

    ReplyDelete
  14. Thanks for the tip. Very convenient. :)

    ReplyDelete
  15. Awesome tip! Thanks!

    ReplyDelete
  16. This comment has been removed by the author.

    ReplyDelete
  17. Here is the .bat file I used

    @ECHO OFF
    %1
    echo.
    PAUSE
    @ECHO ON

    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 given
    pt "$(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.

    ReplyDelete
  18. 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.

    ReplyDelete
  19. Whew, it took me half the day, but donb finally got the job done! Why doesn't Notepad++ just come with a python box to check? They must think I'm smarter than I am.

    ReplyDelete
  20. Oh, found the boxes in the plug-in manager!

    ReplyDelete
  21. 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

    ReplyDelete
  22. 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.

    Not sure about your other question regarding what to uninstall.

    ReplyDelete
  23. How about if your python script needs an argument/s?

    ReplyDelete
  24. 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.

    ReplyDelete
  25. I know this is probably a seriously stupid question... But I can't seem to get this to work, so I'm considering that I might have misinterpreted something here...

    In notepad++ do I actually type "$(FULL_CURRENT_PATH)" or do I replace FULL_CURRENT_PATH with the path to the file I'm trying to run?

    Awaiting a reply. Thanks in advance.

    ReplyDelete
  26. @Anonymous:

    You type "$(FULL_CURRENT_PATH)", as written.

    Notepad++ will then change that variable to the full path of your current open file.

    ReplyDelete
  27. This comment has been removed by the author.

    ReplyDelete
  28. You can use PyNPP Plugin (https://github.com/mpcabd/PyNPP) to achieve this.

    I know this is old but the answer is for people coming from search.

    ReplyDelete