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 :)

44 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. Thank you so much been looking for this!

    ReplyDelete
  4. Works like a charm. Much appreciated.

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

    ReplyDelete
  6. Still helping! Thank you!

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

    ReplyDelete
  8. Awesome! Thanks :-)

    ReplyDelete
  9. Ditto! thank you!

    ReplyDelete
  10. 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
  11. Brilliant, thank you x 100

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

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

    ReplyDelete
  14. Awesome tip! Thanks!

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

    ReplyDelete
  16. 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
  17. 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
  18. 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
  19. Oh, found the boxes in the plug-in manager!

    ReplyDelete
  20. 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
  21. 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
  22. How about if your python script needs an argument/s?

    ReplyDelete
  23. 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
  24. 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
  25. @Anonymous:

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

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

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

    ReplyDelete
  27. 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
  28. 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 have
    C:\Python26\python.exe -i "$(FULL_CURRENT_PATH)"
    and no need for a .bat file.

    ReplyDelete
    Replies
    1. This is helpful but it does not keep it contained in one shell. It creates a new shell every time you run it.
      Suggestions?

      Delete
    2. any auggestions on keeping it in a single shell?

      Delete
  29. thanks. i wish best for u

    ReplyDelete
  30. Just want to comment that I needed to start notepad++ as administrator to have it work.

    ReplyDelete
  31. Its not working for me. The python window just flashes and disappears

    ReplyDelete
    Replies
    1. It worked after restarting
      If the python script has commands to open and write data file, it doesn't work
      I mean the script runs but the data file won't create

      Delete
  32. It still works perfectly in 2020.
    Thx!

    ReplyDelete
  33. Thank you.
    08-2021
    my bat file:
    @ECHO OFF
    C:\Users\xxx\AppData\Local\Programs\Python\Python39\python.exe %1 echo.
    PAUSE
    @ECHO ON

    ReplyDelete
  34. if python is know in the system, all you need is:
    python3 "$(FULL_CURRENT_PATH)"
    and also it helps to put an: input() at the end of your python script to keep the shell open :)

    ReplyDelete