Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Execute a Python Script in Notepad++?

People also ask

How do I run a Python code from a text file?

To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

How do I run code in Notepad?

Step 1 - Open the new notepad with shortcut keys Ctrl+N. Step 2 - Here, we should write the C# code or program. Step 3 - We can save the program at a particular file location with shortcut Ctrl+S. Step 4 - Now, go to Visual Studio 2012 ARM Phone Tool Command Prompt and open the window.

How do I run a .py file directly?

Type cd PythonPrograms and hit Enter. It should take you to the PythonPrograms folder. Type dir and you should see the file Hello.py. To run the program, type python Hello.py and hit Enter.

How do I run a .py file on a PC?

Go to your Start menu (lower left Windows icon), type "Microsoft Store", select the link to open the store. Once the store is open, select Search from the upper-right menu and enter "Python". Select which version of Python you would like to use from the results under Apps.


First option: (Easiest, recommended)

Open Notepad++. On the menu go to: Run -> Run.. (F5). Type in:

C:\Python26\python.exe "$(FULL_CURRENT_PATH)"

Now, instead of pressing run, press save to create a shortcut for it.

Notes

  • If you have Python 3.1: type in Python31 instead of Python26
  • Add -i if you want the command line window to stay open after the script has finished

Second option

Use a batch script that runs the Python script and then create a shortcut to that from Notepad++.

As explained here: http://it-ride.blogspot.com/2009/08/notepad-and-python.html


Third option: (Not safe)

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.

Check if this key exists, and if does not, you could try creating it.


@Ramiz Uddin's answer definitely deserves more visibility :

  • Open Notepad++
  • On the menu go to: RunRun.. (F5)
  • Type in: cmd /K python "$(FULL_CURRENT_PATH)"

Here is what's worked for me:

Open notepad++ and press F5. You'll get a little popup box:

Pop up box for entering the program to run

Type: C:\Python27\python.exe -i "$(FULL_CURRENT_PATH)" for Python 2.7.

and then Save As..., and pick your own key combo to start it each time you want to run something


First install Python from https://www.python.org/downloads/

Run the installer

** IMPORTANT ** Be sure you check both :

  • Install launcher for all users
  • Add Python 3.6 to path

Click install now and finish the installation.

Open notepad++ and install plugin PyNPP from Plugin Manager. I'm using N++ 6.9.2

Save a new file as new.py

Type in N++

import sys

print("Hello from Python!")
print("Your Python version is: " + sys.version) 

Press Alt+Shift+F5

Simple as that.


On the menu go to: "Run" --> "Run..." (or just press F5).

For Python 2 type in:

py -2 -i "$(FULL_CURRENT_PATH)"

For Python 3 type in:

py -3 -i "$(FULL_CURRENT_PATH)"

References:

To understand the py command better:

py -h

Another helpful link to understand the py command: How do I run python 2 and 3 in windows 7?

Thanks to Reshure for his answer that got me on the right track to figure this out.


I also wanted to run python files directly from Notepad++. Most common option found online is using builtin option Run. Then you have two options:

  1. Run python file in console (in Windows it is Command Prompt) with code something like this (links: enter image description here enter image description here enter image description here):

    C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"
    

    (If your console window immediately closes after running then you can add cmd /k to your code. Links: enter image description here enter image description here enter image description here enter image description here) This works fine, and you can even run files in interactive mode by adding -i to your code (links: enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here).

  2. Run python program in IDLE with code something like this (links: enter image description here enter image description here enter image description here enter image description here, in these links C:\Path\to\Python\Lib\idlelib\idle.py is used, but I am using C:\Path\to\Python\Lib\idlelib\idle.bat instead, because idle.bat sets the right current working directory automatically):

    C:\Path\to\Python\Lib\idlelib\idle.bat "$(FULL_CURRENT_PATH)"
    

    Actually, this doesn't run your program in IDLE Shell, but instead it opens your python file in IDLE Editor and then you need to click Run Module (or click F5) to run the program. So it opens your file in IDLE Editor and then you need run it from there, which defeats the purpose of running python files from Notepad++.

    But, searching online, I found option which adds '-r' to your code (links: enter image description here enter image description here enter image description here enter image description here enter image description here):

    C:\Path\to\Python\Lib\idlelib\idle.bat -r "$(FULL_CURRENT_PATH)"
    

    This will run your python program in IDLE Shell and because it is in IDLE it is by default in interactive mode.

Problem with running your python files via builtin Run option is that each time you run your python file, you open new console or IDLE window and lose all output from previous executions. This might not be important to some, but when I started to program in python, I used Python IDLE, so I got used to running python file multiple times in same IDLE Shell window. Also problem with running python programs from Notepad++ is that you need to manually save your file and then click Run (or press F5). To solve these problems (AFAIK*) you need to use Notepad++ Plugins. The best plugin for running python files from Notepad++ is NppExec. (I also tried PyNPP and Python Script. PyNPP runs python files in console, it works, but you can do that without plugin via builtin Run option and Python Script is used for running scripts that interact with Notepad++ so you can't run your python files.) To run your python file with NppExec plugin you need to go to Plugins -> NppExec -> Execute and then type in something like this (links: enter image description here enter image description here):

C:\Path\to\Python\python.exe "$(FULL_CURRENT_PATH)"

With NppExec you can also save your python file before run with npp_save command, set working directory with cd "$(CURRENT_DIRECTORY)" command or run python program in interactive mode with -i command. I found many links (enter image description here enter image description here enter image description here enter image description here enter image description here) online that mention these options, but best use of NppExec to run python programs I found at NppExec's Manual which has chapter 4.6.4. Running Python & wxPython with this code:

npp_console -  // disable any output to the Console
npp_save  // save current file (a .py file is expected)
cd "$(CURRENT_DIRECTORY)"  // use the current file's dir
set local @exit_cmd_silent = exit()  // allows to exit Python automatically
set local PATH_0 = $(SYS.PATH)  // current value of %PATH%
env_set PATH = $(SYS.PATH);C:\Python27  // use Python 2.7
npp_setfocus con  // set the focus to the Console
npp_console +  // enable output to the Console
python -i -u "$(FILE_NAME)"  // run Python's program interactively
npp_console -  // disable any output to the Console
env_set PATH = $(PATH_0)  // restore the value of %PATH%
npp_console +  // enable output to the Console

All you need to do is copy this code and change your python directory if you use some other python version (e.g.* I am using python 3.4 so my directory is C:\Python34). This code works perfectly, but there is one line I added to this code so I can run python program multiple times without loosing previous output:

npe_console m- a+

a+ is to enable the "append" mode which keeps the previous Console's text and does not clear it.

m- turns off console's internal messages (those are in green color)

The final code that I use in NppExec's Execute window is:

npp_console -  // disable any output to the Console
npp_save  // save current file (a .py file is expected)
cd "$(CURRENT_DIRECTORY)"  // use the current file's dir
set local @exit_cmd_silent = exit()  // allows to exit Python automatically
set local PATH_0 = $(SYS.PATH)  // current value of %PATH%
env_set PATH = $(SYS.PATH);C:\Python34  // use Python 3.4
npp_setfocus con  // set the focus to the Console
npe_console m- a+
npp_console +  // enable output to the Console
python -i -u "$(FILE_NAME)"  // run Python's program interactively
npp_console -  // disable any output to the Console
env_set PATH = $(PATH_0)  // restore the value of %PATH%
npp_console +  // enable output to the Console

You can save your NppExec's code, and assign a shortcut key to this NppExec's script. (You need to open Advanced options of NppExec's plugin, select your script in the Associated script drop-down list, press the Add/Modify, restart Notepad++ , go to Notepad++'es Settings -> Shortcut Mapper -> Plugin commands, select your script, click Modify and assign a shortcut key. I wanted to put F5 as my shortcut key, to do that you need to change shortcut key for builtin option Run to something else first.) Links to chapters from NppExec's Manual that explain how to save you NppExec's code and assign a shortcut key: NppExec's "Execute...", NppExec's script.

P.S.*: With NppExec plugin you can add Highlight Filters (found in Console Output Filters...) that highlight certain lines. I use it to highlight error lines in red, to do that you need to add Highlight masks: *File "%FILE%", line %LINE%, in <*> and Traceback (most recent call last): like this.


All the answers for the Run->Run menu option go with the "/K" switch of cmd, so the terminal stays open, or "-i" for python.exe so python forces interactive mode - both to preserve the output for you to observe.

Yet in cmd /k you have to type exit to close it, in the python -i - quit(). If that is too much typing for your liking (for me it sure is :), the Run command to use is

cmd /k C:\Python27\python.exe  "$(FULL_CURRENT_PATH)" & pause & exit

C:\Python27\python.exe - obviously the full path to your python install (or just python if you want to go with the first executable in your user's path).

& is unconditional execution of the next command in Windows - unconditional as it runs regardless of the RC of the previous command (&& is "and" - run only if the previous completed successfully, || - is "or").

pause - prints "Press any key to continue . . ." and waits for any key (that output can be suppressed if need).

exit - well, types the exit for you :)

So at the end, cmd runs python.exe which executes the current file and keeps the window opened, pause waits for you to press any key, and exit finally close the window once you press that any key.