In the previous tutorial, we successfully set up a Raspberry Pi Linux desktop. Let us now start learning Python. We could have jumped right to controlling hardware using Python scripts, but this will defeat the very purpose of using Raspberry Pi and a Very-High-Level-Language (VHLL) like Python to control electronics.
We can easily control electronics and design embedded applications with any microcontroller. The reason we are using Raspberry Pi (a single-board computer) and a Very-High-Level-Language like Python is to utilize features of desktop, web, and network programming in embedded applications. This will let us design powerful and complex applications that may be using user interfaces, graphics, networking, and databases for manipulating, sharing, and utilizing data from embedded sensors and design embedded applications that may be networked to desktop or server systems.
With Raspberry Pi, we can interface a variety of embedded add-on devices (or circuits) that will be providing machine-level data in specific applications. This data can be manipulated and utilized by a VHLL like Python to design large applications in niches like biometrics, robotics, automation, gaming, and cloud computing, etc. For example, we can interface a fingerprint module with Raspberry Pi and design a biometric attendance system. By utilizing features of Python (a High-Level Language), we can design a software solution on this biometric attendance system where an HR manager can view attendance log, evaluate the productivity of employees, calculate salaries and overtime payments, etc. or a complete HR management software.
Even by simply interfacing LED matrix, character LCD, or graphic LCD with Raspberry Pi, we can design a remotely operated display board, where we may be flashing custom messages through an RPi desktop based GUI solution. By interfacing an accelerometer sensor and a couple of buttons, you can design your custom gamepad for gaming on Raspberry Pi. There are unlimited possibilities when you combine electronics, power of the operating system, and features of a powerful high-level programming language like Python. Having a strong foundation of Python will help us build some more than just hobby applications. So, let us first sharpen our Python skills.
Python as programming language
Python is a Very-High-Level Language (VHLL) that can be used to code at a bit-and-bytes level as well. It is a general-purpose, object-oriented, cross-platform programming language that can run on all major operating systems and hardware platforms. Though Python is object-oriented, it is not limited to OOP software design pattern. It is possible to design software solutions on Python using procedural or functional programming, as well. Many software design patterns like Singleton, Model-View-Controller (MVC), Template, Proxy, Command, Observer, Factory, etc. can be implemented in Python. The object-oriented features of Python are similar to C++, though these features are much simpler in Python.
The Python itself is a Very-High-Level Language and has a high level of abstraction from the underlying hardware. Apart from this, Python can be integrated with a variety of other programming languages, where it can be combined with software components written in other languages. This liberty of Python as a programming language has let it find important roles in software solutions of every possible niche.
Python is an interpreted language. So generally, python is slow in comparison to compiled languages. This can be a problem when you design applications that have to respond to time-critical events. However, it is possible to pre-compile python or use modules written in other compiled languages. In most of the cases, the performance of a python application is sufficient.
When we use python for embedded applications on Raspberry Pi, we may face some limitations from both Python as well as RPi. On Python side, the execution of an embedded application may not be as fast as it may be by a machine code on a microcontroller. The Raspberry Pi itself lacks some important hardware features like analog-to-digital converters, real-time clock, and hardware interrupts. The need for analog-to-digital converters or real-time clock can be compensated by interfacing external ADC or RTC with the RPi. For interrupts, you will have to rely on interrupt API of Linux. These software interrupts then need careful management of threads; otherwise, they won’t implement as fast as actual hardware interrupts.
Applications of Python
Python is a general-purpose programming language that can be used in a variety of application domains. It has a standard library and a number of extension modules that let use python in all sorts of applications and software solutions.
By running python on single board computers like Raspberry Pi, Beaglebone, and others in full Linux environments, it can be used in embedded applications. These single board computers can be interfaced with microcontrollers and programmable chips to design interesting applications in embedded systems, robotics, automation, biometrics, and other niches.
A python three implementation called MicroPython can be used to program a microcontroller board named ‘pyBoard’ just like embedded C is used to program Arduino and other microcontrollers. MicroPython on pyBoard lets you program microcontroller applications using Python and let you compile python to bytecode or machine code.
Well, to avoid any confusion, we will be using conventional Python V3 on Raspberry Pi and with the help of some extension modules, will interact with electronic circuits and hardware via GPIO and other hardware interfaces of Raspberry Pi. MicroPython produces a bytecode or executable machine code that can run as firmware on ‘pyBoard’. With Raspberry Pi, we will be designing general-purpose Python applications that will run as Linux processes rather than bytecode or firmware sort of machine code. One advantage of using conventional python on SBC than Micropython on pyBoard is that we get full access to the standard library and all extension modules of python. Micropython lacks most of the features of the standard library.
Still using conventional python, we can import hardware control modules to configure and manage electronics via GPIO and other hardware interfaces. We can use the socket library of python to connect with internet services and can get interpreter access to hardware devices via USB serial port and over Telnet protocol. We can also respond to external devices and timer events with the help of Linux APIs and special python modules. Even we can write power-on boot sequences in python that can implement a python application of any complexity. With such abundant features and extensibility to every possible application domain, python has become the first choice to program the Internet of Things (IoT) applications.
Python, as a general-purpose language, can be used for desktop programming, web development, software development, business applications, and scientific computing. With the help of Tk GUI library and toolkits like Qt, wxWidgets, and Kivy, it is possible to design rich desktop GUIs. The standard library of python supports all major internet protocols and lets us use Python along with HTML, JSON, and XML. There are several python frameworks for web and internet development like Django, Flask, Bottle, Pyramid, and Python-based content management systems like Django CMS and Plone. Python-based tools like SCons, Roundup, Apache Gump, and Buildbot can be used for software development. Python can also be used for Enterprise Resource Planning (ERP) and e-commerce solutions. There are python packages available that can be used for scientific computing, data analysis, and data visualization. No doubt, python has a lot to offer. It is up to the developer on how to use the features and power of python with electronics.
Python has the following four production-quality implementations –
- Classic Python – This is the most up-to-date and full implementation of Python. CPython, including its interpreter, compiler, and standard library and extension modules, is coded in C language. It can run on any C-compliant platform. CPython is the de facto implementation of Python programming language and is best known as simply ‘Python’. It does not need any special programming environment.
- Jython – This is Java implementation of Python developed for Java Virtual Machines. It is compliant with Java 7 and higher. Jython can use all Java libraries and frameworks and can be used in any Java class as an extension module. The latest release of Jython (Jython 2.7.1) is compatible with Python 2.7. Jython still does not support Python V3. It is important to note that V2 and V3 versions of Python are incompatible with each other. Jython can be run on any JVM environment. Jython applications are basically pure Java applications that can run any device having JVM. Java Virtual Machines are available for almost all operating systems and hardware platforms.
- IronPython – IronPython is a .Net (Common Language Runtime) implementation of Python. CLR is now open source and is ported to Linux and macOS as well. CLR also has a cross-platform implementation known as ‘Mono’ that can run on Windows as well as other non-Microsoft operating systems. IronPython can run on any target machine having a .Net framework. It can use any CLR class as an extension module, including classes written in C# and Visual Basic .Net.
- PyPy – PyPy is an alternative implementation of CPython that can generate native machine code just-in-time. PyPy is faster in speed and memory management compared to all other implementations, for which credit goes to its just-in-time compilation to machine code. CPython is faster than Jython and IronPython and can be lagging only by small factors behind PyPy. PyPy is compliant with both Python V2 as well as V3. It supports most of the standard Python libraries and can use any C-coded extensions. By default, it is stack-less and provides micro-threads for concurrency.
Different implementations of Python differ by the environment in which they can run, and libraries, extension modules, and frameworks they can use. The choice of implementation also influences the packaging and distribution of the application. There is also a high-performance implementation of Python known as ‘Pyston’ available. It is possible to install and run any of the above implementations of Python on Raspberry Pi.
By default, RPi uses CPython. For higher performance, PyPy can be installed. PyPy is 2 to 10 times faster than traditional Python. Both PyPy and CPython can use C-coded extension modules. If you need to use Java libraries and modules, Jython can be preferred. For using CLR libraries and extension modules in your Python application, IronPython can be used.
Remember that Raspberry Pi was never meant for time-critical applications. So, we will stick to the default python installation on Raspbian. It is CPython and provides full features of Python language.
Python standard library and extension modules
The standard library of Python includes built-in modules (written in C) for OS-related tasks and modules written in Python for common programming tasks like data handling, string manipulation, date-time operations, mathematical operations, file, and directory access, data compression, etc.
The extension modules, either from the standard library or from specific Python implementation, allows additional functionalities of the underlying operating system or other software components. It is also possible to embed Python in applications coded in other languages via app-specific Python extensions. For example, CPython can use C++ libraries and classes as extension modules; Jython can use Java libraries and classes as extension modules, and IronPython can use CLR libraries and classes as extension modules. On using extension modules, the cross-platform portability of a Python application may be compromised, and the application may require installing additional dependencies accordingly. For example, Jython will require JVM to compile Java libraries and classes.
Python files are normal text files that can be either scripts or modules. Scripts are those files that run directly as a process, while modules are source files that are imported to provide some functionality to the script. A Python file can be both a module as well as a script at the same time as modules can be executable scripts that may provide some functionality to other scripts on import. Any Python application can be seen as a collection of scripts and modules or a script alone.
For software development on Python, one needs a text editor to write code for Python scripts and modules; and a compiler and interpreter to generate executable code. There are several integrated development environments available that combine text editor, compiler and interpreter in a single programming environment. Some of the popular IDEs for Python include IDLE, Eclipse, Geany, and Thonny. Raspbian comes with the default installation of IDLE. Most of the IDEs provide a Python interpreter where Python codes can be executed interactively. These are called interactive sessions. The interactive sessions can be identified by a prompt string ‘>>>’ which indicates that some Python statements are expected here for execution.
Running Python scripts
Python scripts can be run either from within IDE or from a command-line Python interpreter. Within IDE, python statements can be run from an interactive session, as well. Though, it is always better to wrap your application as a Python script. For example, on Raspberry Pi, Python scripts can be run from within IDLE (or other Python IDE) or from the Bash shell itself.
For running Python scripts from the shell, python or python3 command can be used. The command should follow the name and/or path of the target Python script and any optional arguments if required. The bash shell also allows entering multiple lines as a single argument that can be used to execute multiple Python code statements altogether. Some shells may allow a single line command where only one or more Python statements can be executed separated by a semicolon.
The Python script files have a .py extension (suffix to the filename) and complied bytecode files of Python have .pyc or .pyo extensions. For running python scripts from bash shell, following commands can be used –
Python 2.7 can be the default Python version on a machine. On running python command, the script will be executed by default version. On running python3 or python3.5 command, the script will be executed explicitly by V3 or V3.5 versions, respectively, if they are installed.
On Raspbian, Python scripts can be written and compiled using IDLE. You can open IDLE by navigating to Programming -> Python 3 (IDLE).
IDLE will open with an active, interactive session as follows:
For writing a Python script or module, click on Files -> New File.
Write your Python script in the text editor and save it by clicking Files -> Save.
For running a Python script, open it in IDE and click on Run -> Run Module or press F5.
Python is a mature programming language, and there will always be something or the other thing to learn more about Python. The best place to explore about Python is its homepage. Similarly, Jython can be explored from the Jython site, IronPython from .net site, and PyPy from PyPy site. The standard library packages and extension modules can be downloaded from the Python Package Index.
Cost of Python
CPython is covered by the Python Software Foundation License Version 2, which is compatible with GNU Public License (GPL). Jython, IronPython, and PyPy are also covered by similarly liberal licenses. Anything downloaded from the main Python, Jython, IronPython, and PyPy sites is free of cost. Many third-party Python sources, tools, and extension modules that can be downloaded free of cost have similar liberal licenses or are covered by the GPL or Lesser GPL (LGPL). Some commercially developed modules and tools may require payment of a fee anyway or if used for profit. However, Python itself can be used for any proprietary, free, or open-source software development.
In the next tutorial, we will start a discussion on the basics of Python programming language.