Executing Python script from Notepad++

python notepad++ by aipython

Notepad++ is a text editor and source code editor available, officially, only on Microsoft Windows. It is a FREE open-source editor that supports various programming languages and Python is one of them. Notepad++ is one of the widely used source code editors for Python. NPP is the short abbreviation for Notepad++ (Notepad Plus Plus). In our previous tutorial, we have discussed Python IDLE, which is an integrated development environment to edit, execute and debug python code. 

An updated article is live on aipython – Learn to view JSON data with Notepad++

After reading, you will understand various features of Notepad++, how to install Notepad++ on windows, customize and configure NPP for executing python code.


Table of Contents

  1. Notepad++ Features helpful for Python coding
    1. Languages supported by Notepad++
    2. Multiple renaming and full column edit
    3. Multi editing (random selection)
    4. Side by side and Tabbed view
    5. Clone document – edit at two places simultaneously
    6. Search within the same file and in many files
    7. Keyword or Variable highlighting
    8. Autocompletion (Function/word)
    9. Function Lists view in Notepad++
    10. Document map viewer in Notepad++
  2. Downloading and Installing Notepad++ on windows
  3. Styling notepad++ for Python
  4. Configuring Notepad++ for executing Python code
  5. Executing Python Code from Notepad++
    1. Keep the console window appearing
    2. Create a keyboard shortcut to run any Python code
  6. Working with two versions of Python on the same machine using Notepad++
  7. Conclusion

Compared to IDLE, NPP is preferred by the programming community because of its super-cool features. Now a day, developers are choosing Notepad++ for structuring their application, as it is lightweight, multi-language (almost 80 Programming Languages) support and highly flexible, compared to other editors. 

Notepad++ Features helpful for Python coding

Notepad++ Based is based on the powerful editing component Scintilla and is written in C++. NPP uses pure Win32 API and STL (Standard C++ Library ) which ensures a higher execution speed and smaller program size by optimizing as many routines as possible without losing user-friendliness. Notepad++ supports for their syntax highlighting (customizable), syntax folding, auto-completion, function list. Besides syntax highlighting, Notepad++ has many additional features that are especially very useful to programmers. It will allow you to create shortcuts to program calls, such as a Run Python menu item that will invoke python.exe to execute your Python code without having to switch over to another window running a Python shell.

a) Supported Languages

List of all Programming language supported by Notepad++ can be found here, as of March 2020. However, find the word cloud representation of supported language by NPP.

word cloud python notepad++

b) Multiple renaming and full column edit

In multiple renaming, one can select the multiple items (mostly arranged in the same column) and perform the edit. The items can be selected by either pressing and holding Alt key + mouse drag or pressing and holding Alt+Shift+ Arrow key. An animation showing the multiple renaming and column edit is shown below.

column_edit notepad++ python

c) Multi editing (random selection)

Multi edit feature allows users to edit the same text present at multiple locations in a file. Multiple texts can be selected by pressing and holding Crtl + mouse click. Multi edit feature is disabled by default and it can be enabled by navigating to Settings -> Preferences -> Editing -> Select checkbox under the multi-editing section. An animation showing the multiple edit feature is shown below.

multi-edit random

d) Side by side and Tabbed view

To create Dual View, drag and drop any tab that you want it to be in another view (or right-click on the tab) then choose “Move to Other View” command from the popup context menu. Once you’ve got 2 views, you can move files between 2 views by drag-and-dropping. An animation showing side by side (dual view) feature is shown below.

side by side view

e) Clone document – edit at two places simultaneously

Drag and drop any tab that you want to clone (or right-click on the tab) then choose “Clone to Other View” command from the popup context menu. The cloned document is the same as its original one, but with the separated view. An animation showing working on cloned document feature is shown below.

clone-doc-edit

f) Search within the same file and in many files

Similar to other text editors, Notepad++ also provide the feature to search a particular word in the same file or a group of files. Normally the searching process for a specific word in a particular document is pretty simple and the same way as many software, text editor or IDE have. Keyboard shortcut Crtl+F opens up a search dialogue and then the word can be keyed in and the search begins. Now, as far as searching a specific word in multiple documents, or a directory, is concerned, Notepad++ has this feature and can be activated by a combination of Crtl+Shift+F. The complete searching process is demonstrated via the animation provided below.

search in many file notepad++ python

g) Keyword or Variable highlighting

Using a highlighting feature, a programmer can find, where all, a particular word/variable has been used. This comes very handily when you want to edit the code or rename any variable. An animation below represents the highlight feature of Notepad++.

keyword highlight notepad++ python

h) Autocompletion (Function/word)

Notepad++ offers automatic completion of various sorts of text after you have entered an initial substring (or prefix), which can save you having to type all of a long word (and potentially save you mistyping it). You accept the suggestion by typing the Enter or Tab key, and the word is completed within your buffer as if you’d typed it all out. If the suggested word is not what you want, keep typing. The completion list can be triggered automatically as you type, via settings in Settings > Preferences > Auto-Completion: Auto-Completion is enabled by a checkbox.

autocomplete feature notepad++ python

If more than one word in the list of candidate words matches what you’ve typed, Notepad++ will present a list containing the words; the highlighted word in the list is the one that will be selected on pressing the Enter key, but you can use the Down- & Up-arrow keys, or PageDown & PageUp, to move through the list; or, type Esc to dismiss the list.


i) Function Lists

Function List Panel is a zone to display all the functions (or method) found in the current file. The user can use the Function List Panel to access a function definition quickly by double-clicking function item on the list. Function List can be customized to list the functions for whichever language. A detailed method is described here.

function list notepad++ python

j) Document map viewer

Document map help programmer when working with bulky code (> 1000 lines). It creates a compressed view of the document along the side of Notepad++ and provides users with ease of navigation. The highlighted box inside the document map helps to scroll up or down through the document. The image shown below shows a typical document map in NPP.

document map notepad++

Downloading and Installing Notepad++ on windows

Notepad++ is available only for Windows OS. You can download Notepad++ from the official website. After downloading follow the simple and easy process to install it on windows. Step by step installation process is described below.

Double click on the installer [npp.7.8.5.Installer.x64.exe] to get started with the installation process. Please note that the actual version number may be different for you. The first window will ask for choosing the installation language. Click on Ok button and proceed ahead.

install language npp

Next window titled “Welcome to Notepad++ setup“, Just click Next and proceed. The third window will be “License agreement”, click on ‘I Agree’ button to proceed further.

The fourth window asks for the installation location, I would recommend to leave it default and click on the Next button.

select install location npp

The fifth window asks to choose the components that you want to install. Choose ‘Custom’ from the drop-down menu and put a checkmark on all the checkboxes as shown in the image below.

customize install npp

At last, put a checkmark on the checkbox which says to ‘create a desktop icon’ and hit the Install button.

Congratulation!! You have installed Notepad++ 


Styling notepad++ for Python

When you open up NPP for the first time, the interface (mainly editor portion, background colour, font colour and style) will look so dull. As a user, you probably won’t like the interface at all. Notepad++ has provided with hell lot of built-in themes that you can use to enhance the interface. The styling parameters can be accessed by navigating to Settings ->Style Configurator. There are various options available inside the configurator dialogue box such as Themes selector, Font colour, background colour, foreground colour and other language-specific features. The detailed process, to use style configurator, for styling Notepad++ is available in the video provided below. 


Configuring Notepad++ for executing Python code

This is the most important part of this article i.e, Notepad++ Python. Here you will learn to execute python code from Notepad++. I assume you have written a python code in NPP. I will guide you through the complete process, so I need your attention, Please stay with me.

  • Python should be installed on your system (preferably Python 3)
  • Notepad++ should be installed on the same system
  • Write a Python script on NPP and save it in your system (at any location)

To Execute Python script, Navigate to the top menu bar and click on Run from the menu. You will get a drop-down, from the drop-down menu click on the Run option, alternatively press F5 from the keyboard, a new popup window will appear similar to the image shown below.

run menu npp

Now, we need to select Python.exe, installed on your machine, by clicking on the […] button. Navigate to the installed location and select python.exe, as shown in the image below.

python exe location

The complete strings inside this box should be of the following format as shown in the following image. Copy the text followed after the term “Actual” from given below line and paste into Run dialogue.

full exe path

Syntax: python.exe location path of the program code (aka current path)

# Actual
C:\Python374\python.exe "$(FULL_CURRENT_PATH)"

Now, click on the Run button to execute the code. 

IMPORTANT: You will notice that the program has executed very quickly and console (black window) disappeared within a fraction of second. Sometimes you may not even notice the console window.

In the next section, we will see how to configure, execution in, Notepad++ to keep the console window appeared until the user closes it as well as we will create a keyboard shortcut to run any Python code from NPP.

A nice video demonstration of executing Python code from Notepad++.


Executing Python Code from Notepad++

a) Keep the console window appearing

We will edit the syntax that we had used, in the last section, to run python code. The modified syntax now looks like,

with -i option

Syntaxpython.exe location -i path of the program code (aka current path)

# Actual: 
C:\Python374\python.exe -i "$(FULL_CURRENT_PATH)"

“-i” in the middle of the sentence keeps the console window visible, as shown below

keep console open

b) Create a keyboard shortcut to run any Python code

Now, we have verified that the above syntax works perfectly to execute python code. We will bind this to a keyboard shortcut. The keyboard shortcut can be created by clicking on the save button of the Run window dialogue, shown in the image below.

keyboard shortcut

Clicking on the save button will popup a window to decide a shortcut.

  1. Write a convenient name for the shortcut like Run_Python
  2. Click on (any or all) checkboxes to include in the shortcut, I have chosen Ctrl and Alt
  3. From the drop-down menu select any convenient key, I have chosen E. This dialogue will also prevent you from creating any shortcuts which are already available, by throwing a message in the same window.

I had created my keyboard shortcut, Crtl+Alt+E, to execute Python code from Notepad++.

Working with two versions of Python on the same machine

If you have more than one version, or subversion, of Python, is installed on your system then you can create the shortcut for both the versions separately.

Follow the same process to create the shortcut and keep in mind to select respective python.exe path.

I have created two shortcuts, 

For executing Python 3.6.8 -> Crtl+Alt+6
For executing Python 3.8.1 -> Crtl+Alt+8

Conclusion

In this article, you have learnt what is Notepad++, it’s various features such as multiple edits, numerous supported language, syntax highlight, various search option, function list, document map etc.. You have also learned to download and install Notepad++ on windows as well as to style and configure for executing Python scripts. Finally, you learned to manage two versions of python and execute the same python code using two versions of Python. 

Don’t stop your learning here itself, take advantage of your learning and explore further. You can find sample python 3 codes and start practising on Notepad++. 

If you need any further assistance then do not hesitate to write them in the comment below, we are always there to help our community.

Keep Learning and Keep Growing !!


How did you like the content