A program in Python is stored in text files on the device's file system, as Python's source code is interpreted, not compiled, opposite to C++. A typical file extension for programs in Python is .py
.
In the context of IoT programming, both Python and Micropython share the same syntax and mostly the same libraries, so source code, in many cases, is portable. General hardware-related libraries like GPIO handling or timers are shared between those two Python worlds, and hardware-specific differences are minor compared to the Arduino framework.
Python is simple and efficient in programming the not-so-complex IoT algorithms but does not offer the level of control needed in real-time applications. It can be easily used for prototyping, testing hardware and implementing simple tasks.
Nowadays, Python interpreter usually comes with OS (Linux) preinstalled.
The sample installation procedure for Raspbian OS is presented in the manual maintained by the Raspberry Pi manufacturer [1].
In the case of the popular Raspbian or Ubuntu for Raspberry Pi, there are usually 2 versions of Pythons preinstalled: Python 2 and Python 3, because of the historical differences between implementations. Many OS applications are written in Python.
Python version can be started from the terminal simply by calling:
~$ python --version
Python 3.8.10
In the case one needs to use a specific version, you can start the interpreter explicitly referring to Python 2 or Python 3:
~$ python2 Python 2.7.18 (default, Jul 29 2022, 09:29:52) [GCC 9.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> quit() ~$ python3 Python 3.8.10 (default, May 26 2023, 14:05:08) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> quit()
Python can be executed via a desktop graphical interface (in the graphical terminal), in a text-based Linux installation via terminal, or remotely via ssh. As it is possible to write applications with visual GUI, starting them in a non-graphical installation of the Linux OS will throw an error. To execute a Python script (program), one needs to execute the following:
~$ python mypythoniotapp.py
Linux, Windows and Mac systems used to bind a .py
file extension with a default Python interpreter, so it is possible to run Python script directly, either with the use of file manager or execute it from the command line:
~$ ./mypythoniotapp.py
The following chapters present Python coding elements specific to the microcontrollers. A complete Python course is beyond the scope of this publication, but it can be easily obtained online (links presented by the end of the chapter).
A dozen of IDEs can be used to program in Python. The most common are:
The following subchapters present some IoT and embedded systems-specific Python programming and an elementary introduction:
For in-depth Python courses and more, follow the links: