Python Fundamentals for IoT

 General audience classification icon  General audience classification icon
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.

Python is the language of the first choice when it comes to AI applications. Most autonomous devices (such as cars) run, in fact, Python code along with C++, hardware accelerated, on IoT fog class devices such as the NVIDIA Jetson family.
Obviously, Micropython does not contain nor allow the use of very complex libraries and frameworks that sometimes are provided to the developer with only binary backend (that is CPU or MCU specific) such as Tensorflow for AI applications.

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

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:

~$ ./
Note: Python script must be marked as “executable” to run it directly.

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).

IDEs for Python

A dozen of IDEs can be used to program in Python. The most common are:

  • IDLE Editor, formerly delivered with Raspbian OS in the bundle, requires GUI. It is currently obsolete but still popular among hobbyists.
  • Thonny Python IDE, which comes with Raspbian OS, recently took over IDLE.
  • Visual Studio Code with plugins for Python, standard with Arduino framework, that also easily integrates remote Python development - it provides two development scenarios: local on the IoT device (Raspberry Pi, requires GUI) and remote from the PC to the IoT device, that works with headless Raspberry Pi OSes installations.
  • PyCharm Community Edition requires additional installation [2] and requires GUI.
  • Simple code can be authored in the terminal using any text editor (e.g. Nano), as Python source files do not require compilation and are plain text ones. This is not very convenient, but it can help if no dedicated IDE and GUI are available, e.g., for rapid work remotely.

The following subchapters present some IoT and embedded systems-specific Python programming and an elementary introduction:

Additional Resources for Python programming for beginners

For in-depth Python courses and more, follow the links:

  1. The Python syntax and semantics: Python Semantics.
  2. The Python Package Index (PyPi): PyPi.
  3. The Python Standard Library:PSL.
  4. Free online Python course:
en/iot-open/scriptingprogramming/pythonfundamentals.txt · Last modified: 2023/11/23 10:25 by pczekalski
CC Attribution-Share Alike 4.0 International Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0