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 :)
This is exactly what I am looking for.
ReplyDeleteagree - simple and to the point. thank you!
Deletegot what i need! Thanks Dude! :)
Deletethis doesnt work
DeleteThanks! 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.
ReplyDeleteAwesome. Thanks!
ReplyDeleteThank you so much been looking for this!
ReplyDeleteWorks like a charm. Much appreciated.
ReplyDeleteThis is great, thanks! exactly what i needed!
ReplyDeleteStill helping! Thank you!
ReplyDeleteThank you very much, simple, effective and efficient. No more IDLE!
ReplyDeleteAwesome! Thanks :-)
ReplyDeleteDitto! thank you!
ReplyDeleteI 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!
ReplyDeleteBrilliant, thank you x 100
ReplyDeleteThe changing to a bat file to prevent the closing was what I was missing - thanks for the post!
ReplyDeleteThanks for the tip. Very convenient. :)
ReplyDeleteAwesome tip! Thanks!
ReplyDeleteNice one!
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHere is the .bat file I used
ReplyDelete@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.
Thx... Exactly what I was looking for.
ReplyDeleteI 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.
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.
ReplyDeleteOh, found the boxes in the plug-in manager!
ReplyDeleteI just started with Pythn 2 days back. I am using Windows as my OS. I have installed ActiveStateActivePython, Python2.7.3, Notepad++
ReplyDeleteI 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.
ReplyDeleteNot sure about your other question regarding what to uninstall.
How about if your python script needs an argument/s?
ReplyDeleteAn 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.
ReplyDeleteYou 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 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...
ReplyDeleteIn 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.
@Anonymous:
ReplyDeleteYou type "$(FULL_CURRENT_PATH)", as written.
Notepad++ will then change that variable to the full path of your current open file.
This comment has been removed by the author.
ReplyDeleteYou can use PyNPP Plugin (https://github.com/mpcabd/PyNPP) to achieve this.
ReplyDeleteI know this is old but the answer is for people coming from search.
Thanks. Really helpful...
ReplyDeleteI 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:
ReplyDeleteC:\Python26\python.bat "$(FULL_CURRENT_PATH)"
you'd have
C:\Python26\python.exe -i "$(FULL_CURRENT_PATH)"
and no need for a .bat file.
This is helpful but it does not keep it contained in one shell. It creates a new shell every time you run it.
DeleteSuggestions?
any auggestions on keeping it in a single shell?
Deletethanks. i wish best for u
ReplyDeletecool
ReplyDeleteJust want to comment that I needed to start notepad++ as administrator to have it work.
ReplyDeleteIts not working for me. The python window just flashes and disappears
ReplyDeleteIt worked after restarting
DeleteIf 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
It still works perfectly in 2020.
ReplyDeleteThx!
Thank you.
ReplyDelete08-2021
my bat file:
@ECHO OFF
C:\Users\xxx\AppData\Local\Programs\Python\Python39\python.exe %1 echo.
PAUSE
@ECHO ON
if python is know in the system, all you need is:
ReplyDeletepython3 "$(FULL_CURRENT_PATH)"
and also it helps to put an: input() at the end of your python script to keep the shell open :)