Notepad++ Plugin To Run Python Scripts

I use Python everyday, it has become my first tool to use when I need to do anything. In Arabic (in Syrian Arabic specifically) I’d say that Python has become my hand and leg 😛

Yesterday I was writing a small Python script to read the YACC file and generate a list of all the specified rules inside it, so I don’t have to scroll through the long file to find out what rules are inside it 😉

I use Notepad++ as my default text editor on Windows, and I was writing the script using it – Notepad++. I wanted to test if the script is working, so I ran an instance of Command Line Prompt, and as I was going to change the directory to the directory of the script I thought; “Why doesn’t Notepad++ have a Run In Python command in it?”. So as usual I got pissed off and decided to create my own plugin to have that command in Notepad++ 8)

The Reference

I searched on the net, I found this Notepad++ Plugin Template, I edited it a little bit and I managed to do it 🙂

You can read below about the process and how it works, but if you just came here to download it click here.

The Process

To run a Python script; the plugin must do the following:

  • Get the path of the selected file in Notepad++.
  • Get the path of Python executable file, since not everyone has Python in the PATH environment variable.
  • Building the run command.
  • Execute the run command.

Get The Path of The Selected File In Notepad++

I also searched the net and found the following code:

The function sends a message to Notepad++ asking it to save the file before trying to run it, the result of the message will be either 1 which means that the file was saved, or 0 which means that the file wasn’t.
Then the function sends a message to Notepad++ asking it to put the full path of the currently selected file into a specific variable.
Now a little problem occurs when the file is unmodified, and the problem is that the first message will return 0 which means that the file wasn’t saved, while the second message will return a valid file path. So the function will search the path for a backslash which is the path separator in Windows, because a valid full path must contain at least one separator.
When the path is valid and the file is saved the function returns the path and sets the ok parameter to true, while in other cases it will return the path and set ok parameter to false.

Get The Path Of Python Executable File

The plugin will search for Python in its default path (“C:\Python[VER]”). If this method fails the plugin will search in registry for the key.

The Python installation path exists in the Windows Registry, so I wrote a small function to search the registry for the path and return it:

The code above accepts the root key as a parameter, and the code that calls it will call it twice, once with HKEY_CURRENT_USER and the other with HKEY_LOCAL_MACHINE, so it will try to find it in both locations HKEY_CURRENT_USER\Software\Python\PythonCore and HKEY_LOCAL_MACHINE\Software\Python\PythonCore.

Building The Run Command

The plugin allows the user to choose to run the file in interactive mode, so the window won’t disappear after the program is done. I’ve also added another thing, that is the ability to run the file in pythonw.exe instead of python.exe, and that’s for applications with GUI, so you don’t have to see a console window behind your application GUI 😉

This one would be very simple, just concatenate: Python installation path, “python.exe” and the full path of the current file.

Execute The Run Command

This function accepts a path parameter so it will run the process in the specified path (which is the path of the folder that includes the Python file to run) instead of the default path, this allows applications reading/writing files inside the same folder (or a relative path) of the application path to find those files.

This one is also simple, just create a new process with the run command.

Screenshot

PyNPP Notepad++ Plugin Screenshot

License

This work is licensed under GNU General Public License v3.

Download

PyNPP v1.2 on GitHub

GitHub

Plugin DLL:

Plugin DLL

Source Code

Source Code


Update History

  • 18, April 2010: PyNPP will search for Python in its default path C:\Python[VER]\.
  • 13, December 2010: PyNPP will allow the user to run the file in three modes: normal, interactive and GUI. As suggested by Andrew here.
  • 13, December 2010: PyNPP will run the file in its folder, so it can read/write files next to it. As suggested by Andrew here.
  • 03, January 2011: PyNPP will search for Python in both HKEY_CURRENT_USER\Software\Python\PythonCore and HKEY_LOCAL_MACHINE\Software\Python\PythonCore.
  • 05, January 2011: PyNPP will save the file before trying to run it.
    Fixed a bug in setting the current folder while running the process.
  • 12, August 2011: PyNPP allows you to select the Python folder using an options dialog. As suggested by Hönes here.
  • 21, December 2011: PyNPP moved to GitHub.
  • 27, April 2014: v1.1 released, PyNPP kills the Python instance it launches when the file is re-run. As suggested by Michał Kłopot.
  • 30, March 2015: v1.2 released, PyNPP supports debugging the file with PDB. As suggested by fdufnews.

110 thoughts on “Notepad++ Plugin To Run Python Scripts

  1. Hello.
    First and foremost BIG “Thank you!” for your generous work and support.
    I recently started to learn Python and tried your plugin in latest Notepad ++ version(7.2 32bit). I have installed latest version of Python(3.5 32bit) and I have different results when trying my code in the plugin(such as SyntaxErrors) as opposed to the test environment offered by the course(one of the well-known free MOOC courses available on the Net).
    Also, debugging can’t be used as it opens and closes in less than 1 second.
    The only window that remains open is the interactive one but here I have errors…
    Any suggestions please?

    Than you in advance.

Leave a Reply