Primary Menu

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

C++

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

boolpythonExists(std::wstring foldername){

std::wstring path=L"C:\\";

path+=foldername;

path+=L"\\python.exe";

WIN32_FIND_DATA data;

HANDLEh=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"";

boolpythonInstalled=false;

WIN32_FIND_DATA data;

HANDLEh=FindFirstFile(L"c:\\python*",&data);

if(h!=INVALID_HANDLE_VALUE)

{

do

{

if(!(data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY))

continue;

char*nPtr=newchar[lstrlen(data.cFileName)+1];

for(inti=0;i&lt;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));

}

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.

Post navigation

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

Comment navigation

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?