DESK-MX6UL-AN-0004: Using Python for Embedded applications

From DAVE Developer's Wiki
Revision as of 16:22, 19 January 2022 by U0007 (talk | contribs)

Jump to: navigation, search
Info Box


200px-Emblem-important.svg.png

This application note has been validated using the kit version in the History table.

History[edit | edit source]

Version Date Development Kit version

1.0.0

Oct 2021

DESK-MX6UL-L 1.0.1

2.0.0 --- 2022 DESK-MX6UL-L 3.0.0

Introduction[edit | edit source]

As found on Python official website, Python is a programming language that lets you work more quickly and integrate your systems more effectively and also Python can be easy to pick up whether you're a first-time programmer or you're experienced with other languages.

These sentences got a real confirmation if you have a look at the IEEE Top Programming Languages 2021 ranking. As you can see in the following picture Python reached the Top of the ranking even more than the native C language used since the beginning in Embedded systems programming:


IEEE Top programming languages 2021 - Embedded


As reported in this article The C/C++ programming languages dominate embedded systems programming, though they have a number of disadvantages. Python, on the other hand, has many strengths that make it a great language for embedded systems.

This application note provides some examples of software packages installation that can be used for building an Industrial IoT Gateway adding python3 libraries to the SBC Lynx platform.

Python on DESK[edit | edit source]

python3 application is already present on DESK-MX6UL-L dave-image-devel root file system.

Installing python packages[edit | edit source]

First of all check for the default installed python/pip version in the DESK-MX6UL root file system:

root@desk-mx6ul-lynx:~# python3 --version
Python 3.9.4
root@desk-mx6ul-lynx:~# 

pip3 is not present - by default - on standard root file system, but can be easily installed using python:

root@desk-mx6ul-lynx:~# curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0:00:01 --:--:-- 1846k
root@desk-mx6ul-lynx:~# python3 get-pip.py 
Collecting pip
  Downloading pip-21.3.1-py3-none-any.whl (1.7 MB)
     |################| 1.7 MB 2.1 MB/s            
Collecting setuptools
  Downloading setuptools-60.5.0-py3-none-any.whl (958 kB)
     |###########| 958 kB 2.1 MB/s            
Collecting wheel
  Downloading wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel, setuptools, pip
Successfully installed pip-21.3.1 setuptools-60.5.0 wheel-0.37.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
root@desk-mx6ul-lynx:~# 

After the installation, pip3 is the latest version available for Python3 3.9.4:

root@desk-mx6ul-lynx:~# pip3 --version
pip 21.3.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)
root@desk-mx6ul-lynx:~# 

Virtual environments[edit | edit source]

As explained on python3 12. Virtual Environments and Packages tutorial it is better to "create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages"

This avoids the problem related to Applications that sometimes need a specific version of a library.

  • for example, create the virtual environment named desk
root@desk-mx6ul-lynx:~# python3 -m venv desk-env
root@desk-mx6ul-lynx:~# which pip3
/usr/bin/pip3
root@desk-mx6ul-lynx:~# 
  • activate the virtual environment
root@desk-mx6ul-lynx:~# source desk-env/bin/activate
(desk-env) root@desk-mx6ul-lynx:~# which pip3
/home/root/desk-env/bin/pip3
(desk-env) root@desk-mx6ul-lynx:~#
  • update pip3 in the the virtual environment
(desk-env) root@desk-mx6ul-lynx:~# /home/root/desk-env/bin/python3 -m pip install --upgrade pip
Collecting pip
  Using cached pip-21.3.1-py3-none-any.whl (1.7 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 20.2.3
    Uninstalling pip-20.2.3:
      Successfully uninstalled pip-20.2.3
Successfully installed pip-21.3.1
(desk-env) root@desk-mx6ul-lynx:~# /home/root/desk-env/bin/pip3 --version
pip 21.3.1 from /home/root/desk-env/lib/python3.9/site-packages/pip (python 3.9)
(desk-env) root@desk-mx6ul-lynx:~# 

In this way it is possible to install the required packages (with their specific version required) only in this virtual environment (which may differ from the root installation).

pip package installation[edit | edit source]

For the purposes of this Application Note, pip3 has been used to install some python packages typically used in an Industrial Gateway equipment

In this example, the following packages are installed pyserial and pymodbus (for bus communications), flask, fastapi (web framework), bottle (lightweight WSGI micro web-framework), requests (HTTP library), schema, ssdp, scapy (network packet manipulation), nmap, ujson (JSON framework):

(desk-env) root@desk-mx6ul-lynx:~# pip3 install pyserial
Collecting pyserial
  Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
     |################################| 90 kB 780 kB/s            
Installing collected packages: pyserial
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed pyserial-3.5
(desk-env) root@desk-mx6ul-lynx:~# pip3 install modbus
Collecting pymodbus
  Downloading pymodbus-2.5.3-py2.py3-none-any.whl (154 kB)
     |#############################| 154 kB 1.5 MB/s            
Collecting six>=1.15.0
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Requirement already satisfied: pyserial>=3.4 in ./desk-env/lib/python3.9/site-packages (from pymodbus) (3.5)
Installing collected packages: six, pymodbus
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed pymodbus-2.5.3 six-1.16.0
(desk-env) root@desk-mx6ul-lynx:~# pip3 install fastapi
Collecting fastapi
  Downloading fastapi-0.72.0-py3-none-any.whl (52 kB)
     |################################| 52 kB 68 kB/s             
Collecting starlette==0.17.1
  Downloading starlette-0.17.1-py3-none-any.whl (58 kB)
     |###############################| 58 kB 594 kB/s            
Collecting pydantic!=1.7,!=1.7.1,!=1.7.2,!=1.7.3,!=1.8,!=1.8.1,<2.0.0,>=1.6.2
  Downloading pydantic-1.9.0-py3-none-any.whl (140 kB)
     |############################| 140 kB 2.3 MB/s            
Collecting anyio<4,>=3.0.0
  Downloading anyio-3.5.0-py3-none-any.whl (79 kB)
     |###############################| 79 kB 729 kB/s            
Collecting typing-extensions>=3.7.4.3
  Downloading typing_extensions-4.0.1-py3-none-any.whl (22 kB)
Collecting sniffio>=1.1
  Downloading sniffio-1.2.0-py3-none-any.whl (10 kB)
Collecting idna>=2.8
  Downloading idna-3.3-py3-none-any.whl (61 kB)
     |###############################| 61 kB 805 kB/s            
Installing collected packages: sniffio, idna, typing-extensions, anyio, starlette, pydantic, fastapi
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed anyio-3.5.0 fastapi-0.72.0 idna-3.3 pydantic-1.9.0 sniffio-1.2.0 starlette-0.17.1 typing-extensions-4.0.1
(desk-env) root@desk-mx6ul-lynx:~# pip3 install bottle
Collecting bottle
  Downloading bottle-0.12.19-py3-none-any.whl (89 kB)
     |################################| 89 kB 688 kB/s            
Installing collected packages: bottle
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed bottle-0.12.19
(desk-env) root@desk-mx6ul-lynx:~# pip3 install requests
Collecting requests
  Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
     |##############################| 63 kB 124 kB/s            
Collecting charset-normalizer~=2.0.0
  Downloading charset_normalizer-2.0.10-py3-none-any.whl (39 kB)
Requirement already satisfied: idna<4,>=2.5 in ./desk-env/lib/python3.9/site-packages (from requests) (3.3)
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.8-py2.py3-none-any.whl (138 kB)
     |##############################| 138 kB 632 kB/s            
Collecting certifi>=2017.4.17
  Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
     |###########################| 149 kB 547 kB/s            
Installing collected packages: urllib3, charset-normalizer, certifi, requests
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed certifi-2021.10.8 charset-normalizer-2.0.10 requests-2.27.1 urllib3-1.26.8
(desk-env) root@desk-mx6ul-lynx:~# pip3 install schema
Collecting schema
  Downloading schema-0.7.5-py2.py3-none-any.whl (17 kB)
Collecting contextlib2>=0.5.5
  Downloading contextlib2-21.6.0-py2.py3-none-any.whl (13 kB)
Installing collected packages: contextlib2, schema
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed contextlib2-21.6.0 schema-0.7.5
(desk-env) root@desk-mx6ul-lynx:~# pip3 install ssdp
Collecting ssdp
  Downloading ssdp-1.1.0-py2.py3-none-any.whl (4.3 kB)
Installing collected packages: ssdp
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed ssdp-1.1.0
(desk-env) root@desk-mx6ul-lynx:~# pip3 install scapy
Collecting scapy
  Downloading scapy-2.4.5.tar.gz (1.1 MB)
     |#########| 1.1 MB 1.3 MB/s            
  Preparing metadata (setup.py) ... - \ done
Using legacy 'setup.py install' for scapy, since package 'wheel' is not installed.
Installing collected packages: scapy
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
    Running setup.py install for scapy ... / - \ | / done
Successfully installed scapy-2.4.5
(desk-env) root@desk-mx6ul-lynx:~# pip3 install nmap
Collecting nmap
  Downloading nmap-0.0.1-py3-none-any.whl (2.7 kB)
Installing collected packages: nmap
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed nmap-0.0.1
(desk-env) root@desk-mx6ul-lynx:~# pip3 install ujson
Collecting ujson
  Downloading ujson-5.1.0.tar.gz (7.1 MB)
     |###############| 7.1 MB 10 kB/s             
  WARNING: Value for prefixed-purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /tmp/pip-build-env-aajn4vfi/normal/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for prefixed-platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /tmp/pip-build-env-aajn4vfi/normal/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = '/tmp/pip-build-env-aajn4vfi/normal'
  WARNING: Value for prefixed-purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /tmp/pip-build-env-aajn4vfi/overlay/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for prefixed-platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /tmp/pip-build-env-aajn4vfi/overlay/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = '/tmp/pip-build-env-aajn4vfi/overlay'
  WARNING: Value for purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
  WARNING: Value for platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  Installing build dependencies ... - \ | / - \ | / - done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... - \ done
Building wheels for collected packages: ujson
  Building wheel for ujson (pyproject.toml) ... - \ | / - \ | done
  Created wheel for ujson: filename=ujson-5.1.0-cp39-cp39-linux_armv7l.whl size=37764 sha256=10ac0ac0859d098528a5985fb09ddc0dcb2e7682ff9d587cbae8be01dafe3edd
  Stored in directory: /home/root/.cache/pip/wheels/5b/11/cb/2e1acde83fd78adc6581984c55442e63d7595711b0b62d8110
Successfully built ujson
Installing collected packages: ujson
  WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
  distutils: /home/root/desk-env/lib/python3.9/site-packages
  sysconfig: /usr/lib/python3.9/site-packages
  WARNING: Additional context:
  user = False
  home = None
  root = None
  prefix = None
Successfully installed ujson-5.1.0
(desk-env) root@desk-mx6ul-lynx:~# 

check for installed packages and version[edit | edit source]

(desk-env) root@desk-mx6ul-lynx:~# pip3 list
Package            Version
------------------ ---------
anyio              3.5.0
bottle             0.12.19
certifi            2021.10.8
charset-normalizer 2.0.10
click              8.0.3
contextlib2        21.6.0
fastapi            0.72.0
Flask              2.0.2
idna               3.3
itsdangerous       2.0.1
Jinja2             3.0.3
MarkupSafe         2.0.1
nmap               0.0.1
pip                21.3.1
pydantic           1.9.0
pymodbus           2.5.3
pyserial           3.5
requests           2.27.1
scapy              2.4.5
schema             0.7.5
setuptools         49.2.1
six                1.16.0
sniffio            1.2.0
ssdp               1.1.0
starlette          0.17.1
typing_extensions  4.0.1
ujson              5.1.0
urllib3            1.26.8
Werkzeug           2.0.2
wheel              0.37.1
(desk-env) root@desk-mx6ul-lynx:~# 

import packages[edit | edit source]

Finally, after the installation steps, it is possible to import the packages:

(desk-env) root@desk-mx6ul-lynx:~# python3
Python 3.9.4 (default, Apr  4 2021, 18:23:51) 
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import os
>>> import time
>>> import ssl
>>> import nmap
>>> import scapy
>>> import serial
>>> import pymodbus
>>> import schema
>>> import ssdp
>>> import requests
>>> import flask
>>> import bottle
>>> import ujson
>>> import fastapi
>>> 
(desk-env) root@desk-mx6ul-lynx:~#