My Technical Blog
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 Shami 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++ ![]()
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
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:
std::wstring getCurrentFile()
{
TCHAR path[MAX_PATH];
::SendMessage(nppData._nppHandle, NPPM_GETFULLCURRENTPATH, MAX_PATH, (LPARAM)path);
return std::wstring(path);
}
The function sends a message to Notepad++ asking it to put the full path of the currently selected file into a specific variable, and then returns the variable’s value.
Get The Path Of Python Executable File
UPDATE (18/4/2010): I edited this section so 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.
bool pythonExists(std::wstring foldername) {
std::wstring path = L"C:\\";
path += foldername;
path += L"\\python.exe";
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(const_cast<LPCWSTR>(path.c_str()), &data);
return (h != INVALID_HANDLE_VALUE);
}
//This code will be run before the plugin calls the function 'getPythonLocation'
std::wstring pythonLoc = L"";
bool pythonInstalled = false;
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(L"c:\\python*", &data);
if( h != INVALID_HANDLE_VALUE )
{
do
{
if (!(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
continue;
char* nPtr = new char [lstrlen( data.cFileName ) + 1];
for( int i = 0; i < lstrlen( data.cFileName ); i++ )
nPtr[i] = char( data.cFileName[i] );
nPtr[lstrlen( data.cFileName )] = '\0';
if (pythonExists(const_cast<LPWSTR>(data.cFileName)))
{
pythonLoc = L"C:\\";
pythonLoc += const_cast<LPWSTR>(data.cFileName);
pythonLoc += L"\\";
pythonInstalled = true;
break;
}
} while (FindNextFile(h, &data));
}
END OF UPDATE
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:
bool getPythonLocation(std::wstring &amp;loc)
{
HKEY hKey;
if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Python\\PythonCore"), 0,
KEY_READ, &hKey) != ERROR_SUCCESS)
{
return false;
}
DWORD dwIdx=0;
TCHAR szKeyName[1024];
DWORD dwSize=1024;
FILETIME fTime;
TCHAR pyPath[MAX_PATH];
DWORD length = MAX_PATH;
if (RegEnumKeyEx(hKey, dwIdx, szKeyName, &dwSize, NULL, NULL, NULL, &fTime) == ERROR_SUCCESS)
{
HKEY hSubKey;
if (RegOpenKeyEx(hKey, szKeyName, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
{
LONG x = RegOpenKeyEx(hSubKey, TEXT("InstallPath"), 0, KEY_READ, &hSubKey);
if (x == ERROR_SUCCESS)
{
if(RegQueryValueEx(
hSubKey,
TEXT(""),
NULL,
NULL,
(LPBYTE)pyPath,
&length) != ERROR_SUCCESS)
{
return false;
}
loc = loc.append(pyPath);
return true;
}
else
return false;
}
else
return false;
}
else
return false;
}
The code opens “HKEY_CURRENT_USER\Software\Python\PythonCore”, if the key exists it will get the path from the first child key of this key.
Building The Run Command
This one would be very simple, just concatenate: Python installation path, “python.exe” and the full path of the current file.
std::wstring buildRunCommand(std::wstring &filePath, std::wstring &pypath)
{
std::wstring command = pypath;
command += TEXT("python.exe \"");
command += filePath;
command += TEXT("\"");
return command;
}
Execute The Run Command
This one is also simple, just create a new process with the run command.
bool launchPython(std::wstring &command)
{
STARTUPINFOW si;
PROCESS_INFORMATION pi;
memset(&si, 0, sizeof(si));
memset(&pi, 0, sizeof(pi));
si.cb = sizeof(si);
return CreateProcess(
NULL,
const_cast<LPWSTR>(command.c_str()),
NULL,
NULL,
FALSE,
CREATE_DEFAULT_ERROR_MODE,
NULL,
NULL,
&si,
&pi) != 0;
}
Screenshot

License
This work is licensed under GNU General Public License v3.
Download
Plugin DLL:
Source Code:
| Print article | This entry was posted by Abd Allah Diab on November 3, 2009 at 10:25 pm, and is filed under Programming. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |






about 10 months ago
Very good Abd,
It is a nice work, I really do love Notepad++, it is Amazing.
Keep moving forward
about 9 months ago
simply cool…
about 5 months ago
Hello,
Interesting plugin. But unfortunately, I can’t install in the Notepad ++ v5.6.8.
#Message#
This Unicode plugin is not compatible with your ANSI mode Notepad++.
This plugin is not compatible with current version of Notepad++.
Do you have any idea?
Thanks and Regards
about 5 months ago
It seems that you have Notepad++ 5.6.8 (ANSI) installed.
My plugin is a Unicode plugin, so you should install the Unicode version and the plugin will work
But as I know Notepad++ are releasing only Unicode versions nowadays, I have the latest version and my plugin is working.
Try installing the latest version instead of updating may be.
Thanks.
about 5 months ago
Hi Abd,
You are right, I didn’t know there were two versions.
Worked!! =)
I now tested in other machine and show:
“Could not locate Python”. But Python installed C:\Python25
It happened because not found registry “HKEY_CURRENT_USER\Software\Python\PythonCore”
Maybe future version, a user will configure the filename. =)
Appreciate it, good job!!
Thanks Very much
about 5 months ago
I’ll think of it
Thanks for trying Razec.
about 4 months ago
Hi! I’m getting the same error as Razec. However i do have the registrykey and in the key PythonPath it says the right path.
Couldn’t you just make a version that doesn’t check for the path and asumes you have the correct path? That would solve this for people like me and it would probably not be very hard for you to do. And the people who dont have the path set can just do so and solve it.
Thank you!
about 4 months ago
I will do it very soon, I promise
about 4 months ago
I’ve updated the plugin to search for Python in default paths first.
Enjoy
about 3 months ago
Thanks for this Abd!!
It is working great!
about 3 months ago
Congratulations for the excellent job.
Thanks for your update.
It’s working here too.
about 1 month ago
Just FYI, there’s now a full plugin for running Python scripts that integrates closely with Notepad++ – allowing you full access to modify the text and so on. http://npppythonscript.sourceforge.net/
about 1 month ago
Thanks Dave, but are you sure it has the same functionality here? Because as far as I understood the plugin you’re maintaining is to manipulate Notepad++ from inside Python, while my plugin is too simple, it just executes the current file in an external Python shell.
Anyway nice plugin
about 1 month ago
It can do that too – a script like
reload(notepad.getCurrentFilename())
will do it, won’t it?
Anywho – as your plugin is not, as far as i know, on the list – I’ll add it to the plugin manager for you, and the wiki. You might want to post an announcement on the Plugin Development forum on the Notepad++ project page too…
Cheers!