Article written by

A passionate pythonist geek looking for problems, to solve :P

90 responses to “Notepad++ Plugin To Run Python Scripts”

  1. Firas I. Kassem

    Very good Abd,

    It is a nice work, I really do love Notepad++, it is Amazing.

    Keep moving forward :)

  2. Zubin Mithra

    simply cool…

  3. Razec

    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

  4. Razec

    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

  5. Notepad++ Rodando Python Scripts « Razec's LAB

    [...] que fiz acima é funcional mas existe uma maneira melhor ainda. A solução foi desenvolvida por Abd Allah Diab que construiu um plugin que roda o script dentro do Notepad++. Solução OpenSource inclusive com [...]

  6. Alex

    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!

  7. Felipe

    Thanks for this Abd!!
    It is working great!

  8. Razec

    Congratulations for the excellent job.
    Thanks for your update.
    It’s working here too. ;)

  9. Banthrass.de » Blog Archive » Python: Notepad++ mit Python verwenden

    [...] Plugin zum Ausführen von [...]

  10. Dave Brotherstone

    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/

  11. Dave Brotherstone

    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!

  12. Oleksiy

    I created myself HKEY_CURRENT_USER\Software\Python\PythonCore with proper path to Python, but Ur pligin still don’t work and I get “Could not locate Python”
    What it can be? :(

  13. andrew

    Great plugin! Though one little thing that bugged me was that when you run a python program that is reading/writing files in the same directory as the python source file (e.g. “./myfile.txt”), it would look for it under the directory containing the python.exe.

    So I modified your source a bit, to use the right path.

    in the PluginDefinition.cpp file, I modified:

    bool launchPython(std::wstring &command, std::wstring &path) //added the path param
    {
    STARTUPINFOW si;
    PROCESS_INFORMATION pi;
    memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    si.cb = sizeof(si);

    return CreateProcess(
    NULL,
    const_cast(command.c_str()),
    NULL,
    NULL,
    FALSE,
    CREATE_DEFAULT_ERROR_MODE,
    NULL,
    const_cast(path.c_str()), //and put it in here
    &si,
    &pi) != 0;
    }

    //and under the void runFile() method I added:

    std::wstring path = L”";

    for(int i=file.length()-1;i>=0;i–) {
    if(file[i] == ‘\\’ || file[i] == ‘/’) {
    path = file.substr(0,i);
    break;
    }
    }

    //MessageBox(GetDesktopWindow(),path.c_str(),path.c_str(),0);

    if(!launchPython(command, path))

    Also this is optional, I modified this method to make the output window stay open after execution:

    std::wstring buildRunCommand(std::wstring &filePath, std::wstring &pypath)
    {
    std::wstring command = pypath;
    command += TEXT(“python.exe -i \”"); //I just added -i here
    command += filePath;
    command += TEXT(“\”");
    return command;
    }

    I’d be very happy if you added these changes, thanks!

  14. Bernard

    Looks nice, but I think the NppExec plugin already does this.

  15. M

    I guess I have another problem. What to do with message: “Couldn’t launch Python”? Paths in register are ok.

  16. M

    Yup, I downloaded it today, just few minutes ago.

  17. cartpauj

    I’m just getting started with Python and found your plugin here for Npp — however it appears that it opens and runs the script in the windows command prompt. Is there any way to make this plugin run the scripts in the actual Python shell instead?

    Thanks for the awesome plugin BTW!

  18. cartpauj

    DOH! That’s embarrassing. Yeah IDLE is what I meant…but I guess that would defeat the purpose of this plugin to begin with so nevermind. I’m learning slowly here :D — thanks again!

  19. Anonymous

    Just wanted to say thanks, just what I was looking for.

  20. Cees Timmerman

    Unlike NppExec, this just works! Thank you!

  21. DevMan

    Actually, I’d like to thank you for this great plugin it saved my time.

    I’ve one problem that the python script runs only under the command window instead of running into the python shell.
    I tried to run it under python shell From Plug-ins > PyNPP > Run file in Pythonw and nothing happen.

    you’re help is highly appreciated.

  22. Brandon L

    I wanted to keep the console open after running the script so I just added the “cmd /k ” command to the beginning of the buildRunCommand string variable.

    std::wstring buildRunCommand(std::wstring &filePath, std::wstring &pypath, bool isW = false, bool isI = false)
    {
    std::wstring command = TEXT(“cmd /k “) + pypath;
    command += TEXT(“python”);
    if (isW)
    command += TEXT(“w”);
    command += TEXT(“.exe “);
    if (isI)
    command += TEXT(“-i “);
    command += TEXT(“\”");
    command += filePath;
    command += TEXT(“\”");
    return command;
    }

  23. Hönes

    I have several Python version installed, cause of the different packages. But your plugin chooses automatically Python26.
    How can I change this. A feature, where I can change it by hand would be nice.

  24. Hönes

    I have found out, that your plugin chooses the first python in a row (I know you knew that already). As I renamed Python 32 in Python25, it choose the one I wanted. Of course that is no optionor solution. I think a lot of people would appreciate a simple version switch. Even in Ellipse it is a little bit clumsy. Just tell you that, because it is not just me, but you would advance your plugin over the average level. Only since your plugin I code Python in notepad++. Take your time, cause it could be a Killer feature for Notepad++, I am looking forward to it. Thanks a lot for your fast response.

  25. Hönes

    Thank you very much. Now I can switch between the different Python versions and play with it. Now notepad++ is the scripting editor of my choice. Grat work.

  26. JF

    Hi ,

    Excellent. Exactly what I needed.

    Thank you!!!!

  27. The Inflatable Mouse

    Hey bud,

    I’m liking your plugin but unfortunately it’s not usable for the way I work with Python/Notepad++.

    The way I work now (and I believe many others work like this too) is that I have NPP open and a command line open, and use command history to repeat the last command to run my script after hitting Ctrl-S to save, sometimes editing the command line to include -i to troubleshoot.

    what would be ideal is if the plugin could be configured to reuse an already open command prompt to spawn the run command and leave the CMD open.

    When I run the script from NPP with your plugin, it flashes a window and closes it again. I’m not a fan of having to edit my code to force a window to remain open I believe this should be functionality of the plugin.

    Just my 2 cents bud,
    Thanks for sharing your work!

  28. UK

    Hey,
    I am new to Python but have qute some knowledge in R.
    I really would like to use your script to execute and check my script (like NppToR). But unfortunately, when I start in interactive mode, python opens but my script is not running. What’s wrong?
    Cheers, UK

  29. dohfOs

    Hey! Lovely little plugin you have here. Works fine for me but with one issue. Would it at all be possible for you to add the utf-8 (or similar) character encoding to it so that the prompt can print foreign letters such as European Å, Ä and Ö’s (etc). Would be great as this is currently keeping me from using this plugin as much as I would’ve liked to.

    Cheers

  30. b0b

    Thanks, works perfectly! Exactly what I needed, the IDLE is so unfriendly.

  31. Aaron

    Simpler way:

    Go to Run -> “Run…”, then type in:

    C:\PythonXX\python.exe “$(FULL_CURRENT_PATH)”

    Save As – call it ‘Run file with Python’ and assign Ctrl+Alt+F5 shortcut….

    Add the -i switch and save as ‘Run with Python Interactive’ with Alt+Shift+F5 as shortcut

    Change exe to pythonw and save as …

    Well, you get the idea… 1 less level of Menu access to, if you don’t like shortcut key combos

    Also, just specify the Python exe in the ‘Run…’ dialolg, save it as ‘Run Python’ with a shortcut like Ctrl+Alt+P to get a bare interactive python shell…

    I suppose the plugin is easier if you need to reinstall…

  32. Abu Ajaj

    I am using it too. A big complement for these great efforts :)

  33. Thomas

    Abd Allah Diab, thank you for this simple and very usable script. :)

  34. Er Ayush Jain

    I tried to use your plugin for 2.7.3 version but it shows error => Couldn’t launch Python” path is right

  35. kalmaro

    Hey, so I was using this plugin and, let me say, this plugin is AMAZING! I love using notepad ++

    But anyway, was using this plugin and couldn’t help but notice that it doesn’t seem to want to run pythonw!

    I’m using the latest python build and just got your plugin today so everything is current. This isn’t technically that big of a deal, unless I want to debug something, which means I’d have to open python up which SORT OF defeats the purpose of using notpad++.

    I’ll still use your plugin since… well it rocks but if you could look into this I would really appreciate it.

  36. Rufus

    I’ve always loved Notepad++, but never explored the depths of plug-ins. Shame on me, for all my time lost if I had only known about the Python linkages such as yours! This was so easy, that I suspect npp will now be my 100% editor for python programs. (I normally bounce between npp, spe, idle).. Between your plugin and the “Python Script” python console within npp, I’m a happy camper. I can’t wait to program exotic search-and-replace, or line extraction/bookmarking macros…

Leave a Reply