30 Mar 2013

Running python script in Notepad++

Notepad++ is a prefered tool for writing a code. I like it personally , and many developers do. It has rich set of features which comes handy while coding. Before this i was using the command prompt (windows) to run my python scripts. In there i have to locate the path of the python file and issue command >>python hello.py every single time.
 In notepad++ , there is a menu option called "Run". I used this menu option to run my python script. Its pretty simple and easy , if you have set the PATH environment variable for
python correctly.

How to do it  ?
Create a batch file for python.exe and a shortcut in Notepad++. Notepad++ will execute that batch file passing the name of the python script. Dead simple.
Lets walk through these simple steps.

Step 1: Create the .bat file

                    @ECHO OFF
                    C:\Python27\python.exe "%1"
                    echo.
                    PAUSE
                    @ECHO ON

 In line 2 installed location of pyhton.exe file is to be specified. This batch file will invoke the python interpreter passing the "%1" as a argument. The "%1" will be replaced by the name of the python script file , when this batch file is executed.

Step 2: Use "Run" menu option of Notepad++ to create the shorcut

You can also press F5 button as short-cut for "Run". a shortcut window will appear.
In textfiled,  type the location of the bactch file created in above step , space and "python_script_location" like
                    C:\Python27\pyth.bat "E:\Code\python\hello.py"
 
           
      
                   
Don't..  Don't press 'Run' , yes I'm serious. It would not run the script. First create the shortcut by pressing the 'Save' a dialog will appear to specify the shortcut to run "hello.py". This shortcut is not saved at physical location on machine rather internally by Notepad++.  A small window will appear like this
    

 'Name" will be a just label for created shortcut. Be careful to choose a shortcut name as it might already be used by your OS or Notepad++ . Once you clicked 'OK' you will see shortcut name appearing in 'Run' menu list (at bottom).
     

Step 3: Just run and have fun ...!!

Open Notepad++ and type your shortcut. Command prompt window will open showing the result of your python script. You can also run python script by clicking on the shortcut name appearing at bottom of 'Run' menu list.
       
simple and easy. Now code and run in fom Notepad++

No comments:

Post a Comment