In this tutorial, we will check how to write Python scripts and run them using IDLE, a Python IDE. This tutorial was tested on a Raspberry Pi 3 model B+, running version 4.9 of Raspbian, installed using NOOBS.
In this tutorial, we will check how to write Python scripts and run them using IDLE.
As we have seen in the previous tutorials, we can easily run Python commands on a Python interpreter, either on IDLE or on the command line. Nonetheless, if we want to write larger and more complex programs, it becomes impracticable to send the commands one by one.
Furthermore, if some command takes a while to run, it is not feasible to have someone waiting for it to finish to send the next commands.
Thus, the most practical way of running Python code is on a script, which is a sequence of instructions that will be interpreted by the Python interpreter. In this case, we don’t need to manually send the instructions one by one.
Python scripts can be written in a text file, with a .py extension, and can run from the command line. Nonetheless, in this tutorial, we will check how to do it with IDLE, a Python IDE (Integrated Development Environment).
Running a script
Since we are going to use IDLE, the first thing we need to do is opening this IDE. I’m using Python 3.x, but the procedure to run the script on Python 2.x should be similar.
So, to open IDLE, go to the main menu and select Programming. Then, on the list of tools that will be displayed, choose Python 3 (IDLE), as indicated in figure 1.
Figure 1 – Opening the IDLE IDE.
After the IDE opens, select the File menu on the top left corner, as indicated in figure 2.
Figure 2 – Opening the File menu on IDLE.
There, select the New File option, which should open a new text editor window. There we can write our script, which will correspond to the Python instructions we want to run.
For testing purposes, you can use the code below. It basically prints a “Hello World” message and then has a loop between 0 and 9 where it prints the value of each iteration of the loop. You can read more about the Python range function here.
print("Hello World") for x in range(0,10): print(x)
You should end with a result similar to figure 3, which shows the previous script written on the editor. Note the Python syntax color highlighting that we get when writing the script on IDLE.
Figure 3 – Writing the script on IDLE.
Now, to run the script, simply click in the Run menu and select Run Module. An alert box asking to save the file will pop, since we haven’t yet saved the file. This is shown in figure 4.
Figure 4 – Script saving alert window.
On the alert window click the Ok button. As shown in figure 5, it should open the file explorer, where we can choose a location to save the script. I’ve saved mine in the Desktop. We also need to name the file. I’ve named it test, but you can call it whatever you want.
Figure 5 – Saving the script file.
After clicking the Save button, the file should get saved with a .py extension on the folder selected. The script should run automatically, with the output being printed to the IDLE Python shell, as shown in figure 6.
Figure 6 – Output of the script on the Python shell.
- Raspberry Pi 3 Raspbian: Running Python from the command line
- Raspberry Pi 3 Raspbian: Python Hello World with IDLE