Executing Python programs can also be done using SSH, and this method is identical to the terminal method. As stated before, some of these other methods may have issues with missing libraries, which is why it is recommended to run Python programs using the terminal window. There are a number of ways to run Python programs on the Raspberry Pi. With the program loaded, click Run > Run current script. In the IDE, click File > Open and then navigate to your Python program. The ls command lists the contents of the current directory. For this purpose, well use Pyserial and its terminal tool.
In this guide, Ill show you how to connect your Raspberry Pi to a serial USB port and read its values with Python from the terminal (without a desktop environment). This creates a new empty file named test.txt. You can also use Raspberry Pi to dialog with some deviceslike Arduinoby using a serial USB port. In a terminal window, enter the following: touch test.txt. First, click the Raspbian logo and then navigate to Programming > Thonny Python IDE. The easiest way to create a new file in Linux is by using the touch command. This IDE can also be used to run Python programs. Thonny is another Python IDE that boasts a number of features, including a powerful debugger that can be used to step through code and watch variables. When the file opens, you can run the program by clicking Run > Run Module or by pressing F5 on the keyboard. With the IDE loaded, click File > Open and then navigate to your Python program. To open this, click the Raspbian logo and then navigate to Programming > Python 3 (IDLE). Raspbian comes with a number of IDEs for programming, and one of these IDEs is the official Python IDE. Or, if your file contains spaces, it will be: This command will execute the Python file that is stated after the command, as shown here: If your Python program is not listed in the list of files, you will need to go back and find where your Python program resides.Īt this point, we can now execute the Python program by using the “python” command. To confirm that your Python program resides in this directory, you can use the command “dir”, and this will list all files and folders in that directory.
Open a terminal window and use the “cd” command to change the working directory to the directory that contains your Python program. For this example, my Python program is located in Documents > Python > Main.py.
Python programs can be executed using a terminal window, but before we open the terminal, we need to know the full path to the Python program. Therefore, it is recommended that you use the terminal window to run Python programs that require external libraries installed using PIP. This may be due to the fact that the Python IDE programs have their own local copy of Python.
Installing additional libraries can be done using PIP, but these seem to only install libraries for programs running in the terminal window. It appears that, on Raspbian, a Python program that needs external libraries may not work using all the methods listed below. In this how-to article, we will look at how to run Python programs on the Pi. Python is a fantastic language for programming the Pi, thanks to its ease of use and access to hardware, including GPIO.