Saturday, December 15, 2012

Configure Notepad++ to run a python script

Install python (2.7.3)

  1. Download the python 2.7.3 windows installer from http://www.python.org/download/
  2. Run through the installer using the default settings for everything.  It should install python in the folder: C:\Python27.  If you're using a different version of python, just substitute out the version number wherever you see 27. 

Install Notepad++

  1. From http://notepad-plus-plus.org/download, download the Notepad++ installer.
  2. Run through the installer using the default settings for everything.
  3. Open Notepad++

Configure Notepad++ to run a python script

  1. Open up a command prompt
  2. Run the following command: 
set PATH=%PATH%;C:\Python27
Note: If your version of python was installed in a different location, make sure to substitute out that location with the one in the command.
You can use the following command to make sure the python path was added to the PATH variable:
echo %PATH% 
You should see the location of the python directory at the end of the output.
  1. Switch back to Notepad++, and go up to the menu bar, select Run, and then select Run... (F5)
  2. Insert the following in the text box that appears:
    C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)"
  3. Click Save...
  4. Configure CTRL+R to be the command.  Give it a name, and press OK.
Now whenever you want to run the python script you're working on, press CTRL+R and it will be executed.  Also you should see the window stay opened after command execution for debugging purposes because of the -i flag. Remove it if you don't want that feature.

26 comments:

  1. I have a little problem -- I am loading an image from "data/img.png". If I run this in IDLE, it works. But when I try it in N++, it require full path ("C:/python27/pjcts/data/img.png"). Can u help me please?

    ReplyDelete
    Replies
    1. Pathing on windows is a little off. I typically use the following code to navigate to the current directory, and then locate a file:

      import os
      mypath = os.path.dirname( os.path.realpath( __file__) )
      image_path = os.path.join(mypath, 'data/img.png')

      Delete
    2. Ok, I will try it. Thanks :)

      Delete
    3. CORRECTION:
      set PATH=%PATH%;C:\Python33
      is the correct command line. You forgot the "=". Thanks for your post.

      Delete
    4. Edited! Thanks for the correction.

      Delete
    5. You're welcome. Looked all over Google. Yet I can't seem to make it work. I did all you said. I'm trying to run a simple search/replace script called scriptest.py containing only
      # Python regular expressions search and replace
      editor.pyreplace(r"old", r"new")
      on a textfile.txt file opened in Notepad++.
      I just don't find out how to make it work. if my script is correct, which notepad tab I should let open, if i should call for
      notepad.open('textfile.txt') in the script.
      if Full_CURRENT_PATH is a variable and should be changed by the exact path and to what? the script? or leave it litteral as is. If you can help. thanks a lot.

      Delete
    6. Full_CURRENT_PATH should be literally entered as shown, and try to run it on a simple print statement to see if it works.

      I've never tried using it to edit the current open document. I would create a path to that text file like I mentioned to the other guy above.

      Delete
    7. Thanks. simple print statement? I don't understand anything after. It is really not clear what is variable and depending on our case or not. and the "-i" parameter does not let the debug log window opened so I don't know squat of what's going on. It closed instantly. My test.py and textfile.txt are in their own folders. (I don't like to put my files in the program directories). I just can't manage to make anything work. my case seems so trivial. I'm disappointed by the complexity of this. this is a nightmare. I just want the trivial change/replace script for a document with notepad. noone seems able to explain such trivial task. desperate!

      Delete
    8. There is a solution here: http://npppythonscript.sourceforge.net/

      Delete
    9. use FULL_CURRENT_PATH instead of Full_CURRENT_PATH

      Delete
    10. Thanks, simple typo. It's fixed now.

      Delete
  2. This works, but it opens a new instance of python.exe every time I run the code. I want it to run the code within the same black box...Any ideas please?

    ReplyDelete
  3. khave, use the cool IDE PyScripter.

    ReplyDelete
  4. For some reason my Notepad forgets this saved path each time - would entering "set PATH=%PATH%;C:\Python27" into the CMD prmopt make any difference?

    ReplyDelete
    Replies
    1. It should stay in your environment path unless that's getting cleared somehow. I'm not sure, but I've since switched from notepad++ to Sublime Text.

      Delete
  5. Thanks guy, being new in python this post has really helped me. KUDOS!

    ReplyDelete
  6. when I hit the button ctrl + R to run, the output screen comes and goes off in a flash. Can't look at the output.

    ReplyDelete
  7. Great, never knew Notepad++ does this!

    ReplyDelete
  8. Thanks. I wish I would've decided to look this up before doing over a half of course on Python.

    ReplyDelete
  9. Is there a way to add the save command before executing the program?

    ReplyDelete
  10. I have a problem with my editor. When I run a python code, my command line pops up for a second and then automatically gets closed.

    ReplyDelete
  11. Whenever i run it a black box comes up and then vanishes so fast any help please?

    ReplyDelete
  12. Bet365 Casino | Claim Your Bonus Now | Deposit £10
    As a result, titanium hair we know that you can win at edc titanium bet365 Casino in 2021! Find out if w88 the casino titanium tools has 벳 인포 an exclusive sign up bonus and you can get up to £30 free

    ReplyDelete