DESK-XZ7-L-AN-0004: Using Python with BORA

From DAVE Developer's Wiki
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
Issue Date Notes

2022/10/25

DESK-XZ7-L-1.0.0-rc1 release

2024/01/30

DESK-XZ7-L-1.0.1 release
2025/06/12 DESK-XZ7-L 2.x.x release


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 2024 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 a Python development system adding python3 libraries to the BORA platform.

Python on DESK[edit | edit source]

python3 application is already present on DESK-XZ7-L petalinux root file system.

Installing python packages[edit | edit source]

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

root@bora:~# python3 --version
Python 3.12.6
root@bora:~#

pip3 can be installed using python and the related script get-pip-py:

root@boralite:~# wget https://bootstrap.pypa.io/get-pip.py
Connecting to bootstrap.pypa.io (151.101.192.175:443)
wget: note: TLS certificate validation not implemented
saving to 'get-pip.py'
get-pip.py           100% |********************************************************************************************************************************************************************************************| 2225k  0:00:00 ETA
'get-pip.py' saved
root@bora:~#
root@bora:~#
root@boralite:~# python3 get-pip.py
Collecting pip
  Downloading pip-25.1.1-py3-none-any.whl.metadata (3.6 kB)
Downloading pip-25.1.1-py3-none-any.whl (1.8 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.8/1.8 MB 4.2 MB/s eta 0:00:00
Installing collected packages: pip
Successfully installed pip-25.1.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
  • then, pip version can be checked
root@bora:~# pip3 --version
pip 25.1.1 from /usr/lib/python3.12/site-packages/pip (python 3.12)

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

First of all the venv module should be installed:

root@bora:~# python3 -m pip install --user virtualenv
Collecting virtualenv
  Downloading virtualenv-20.31.2-py3-none-any.whl.metadata (4.5 kB)
Collecting distlib<1,>=0.3.7 (from virtualenv)
  Downloading distlib-0.3.9-py2.py3-none-any.whl.metadata (5.2 kB)
Collecting filelock<4,>=3.12.2 (from virtualenv)
  Downloading filelock-3.18.0-py3-none-any.whl.metadata (2.9 kB)
Collecting platformdirs<5,>=3.9.1 (from virtualenv)
  Downloading platformdirs-4.3.8-py3-none-any.whl.metadata (12 kB)
Downloading virtualenv-20.31.2-py3-none-any.whl (6.1 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1/6.1 MB 1.7 MB/s eta 0:00:00
Downloading distlib-0.3.9-py2.py3-none-any.whl (468 kB)
Downloading filelock-3.18.0-py3-none-any.whl (16 kB)
Downloading platformdirs-4.3.8-py3-none-any.whl (18 kB)
Installing collected packages: distlib, platformdirs, filelock, virtualenv
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━ 3/4 [virtualenv]  WARNING: The script virtualenv is installed in '/home/root/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed distlib-0.3.9 filelock-3.18.0 platformdirs-4.3.8 virtualenv-20.31.2
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, possibly rendering your system unusable. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv. Use the --root-user-action option if you know what you are doing and want to suppress this warning.
root@bora:~#
  • the virtual environment named desk can be created:
root@bora:~# python3 -m venv desk
root@bora:~# 
  • activate the virtual environment
root@bora:~# source desk/bin/activate
(desk) root@bora:~# 
  • upgrade pip3 in the virtual environment:
(desk) root@bora:~# pip3 install --upgrade pip
Requirement already satisfied: pip in ./desk/lib/python3.12/site-packages (24.2)
Collecting pip
  Using cached pip-25.1.1-py3-none-any.whl.metadata (3.6 kB)
Using cached pip-25.1.1-py3-none-any.whl (1.8 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 24.2
    Uninstalling pip-24.2:
      Successfully uninstalled pip-24.2
Successfully installed pip-25.1.1
  • setuptools can be upgraded too:
(desk) root@bora:~# pip3 install --upgrade setuptools
Collecting setuptools
  Downloading setuptools-80.9.0-py3-none-any.whl.metadata (6.6 kB)
Downloading setuptools-80.9.0-py3-none-any.whl (1.2 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 1.5 MB/s eta 0:00:00
Installing collected packages: setuptools
Successfully installed setuptools-80.9.0
(desk) root@bora:~#
(desk) root@bora:~#

Afterword, the wheel package is worth to be installed for further package installation:

(desk) root@bora:~# pip3 install wheel
Collecting wheel
  Downloading wheel-0.45.1-py3-none-any.whl.metadata (2.3 kB)
Downloading wheel-0.45.1-py3-none-any.whl (72 kB)
Installing collected packages: wheel
Successfully installed wheel-0.45.1
(desk) root@bora:~#

packages installation[edit | edit source]

For the purposes of this Application Note, pip3 has been used to install some python packages typically used in a controller equipment.

In this example, the following packages are installed:

  • netifaces (network interface info)
(desk) root@bora:~# pip3 install netifaces
Collecting netifaces
  Downloading netifaces-0.11.0.tar.gz (30 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: netifaces
  DEPRECATION: Building 'netifaces' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possible replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'netifaces'. Discussion can be found at https://github.com/pypa/pip/issues/6334
  Building wheel for netifaces (setup.py) ... done
  Created wheel for netifaces: filename=netifaces-0.11.0-cp312-cp312-linux_armv7l.whl size=35099 sha256=7535ebe141f50079354d503d093435807ff16974a23d666cef737b7ceddae922
  Stored in directory: /home/root/.cache/pip/wheels/63/fa/57/da80d0ffc8f993315c479b7cd4c8fb1c23910c8baccf6b1b27
Successfully built netifaces
Installing collected packages: netifaces
Successfully installed netifaces-0.11.0
(desk) root@bora:~#
  • asyncua (OPCUA client and server library)
(desk) root@bora:~# pip3 install asyncua
Collecting asyncua
  Downloading asyncua-1.1.6-py3-none-any.whl.metadata (9.6 kB)
Collecting aiofiles (from asyncua)
  Downloading aiofiles-24.1.0-py3-none-any.whl.metadata (10 kB)
Collecting aiosqlite (from asyncua)
  Downloading aiosqlite-0.21.0-py3-none-any.whl.metadata (4.3 kB)
Collecting cryptography>42.0.0 (from asyncua)
  Downloading cryptography-45.0.4-cp311-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl.metadata (5.7 kB)
Collecting pyopenssl>23.2.0 (from asyncua)
  Downloading pyopenssl-25.1.0-py3-none-any.whl.metadata (17 kB)
Collecting python-dateutil (from asyncua)
  Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB)
Collecting pytz (from asyncua)
  Downloading pytz-2025.2-py2.py3-none-any.whl.metadata (22 kB)
Collecting sortedcontainers (from asyncua)
  Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl.metadata (10 kB)
Collecting typing-extensions (from asyncua)
  Downloading typing_extensions-4.14.0-py3-none-any.whl.metadata (3.0 kB)
Collecting cffi>=1.14 (from cryptography>42.0.0->asyncua)
  Downloading cffi-1.17.1.tar.gz (516 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Collecting pycparser (from cffi>=1.14->cryptography>42.0.0->asyncua)
  Downloading pycparser-2.22-py3-none-any.whl.metadata (943 bytes)
Collecting six>=1.5 (from python-dateutil->asyncua)
  Downloading six-1.17.0-py2.py3-none-any.whl.metadata (1.7 kB)
Downloading asyncua-1.1.6-py3-none-any.whl (1.1 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 1.3 MB/s eta 0:00:00
Downloading cryptography-45.0.4-cp311-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl (3.9 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 3.9/3.9 MB 1.0 MB/s eta 0:00:00
Downloading pyopenssl-25.1.0-py3-none-any.whl (56 kB)
Downloading typing_extensions-4.14.0-py3-none-any.whl (43 kB)
Downloading aiofiles-24.1.0-py3-none-any.whl (15 kB)
Downloading aiosqlite-0.21.0-py3-none-any.whl (15 kB)
Downloading pycparser-2.22-py3-none-any.whl (117 kB)
Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
Downloading six-1.17.0-py2.py3-none-any.whl (11 kB)
Downloading pytz-2025.2-py2.py3-none-any.whl (509 kB)
Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)
Building wheels for collected packages: cffi
  Building wheel for cffi (pyproject.toml) ... done
  Created wheel for cffi: filename=cffi-1.17.1-cp312-cp312-linux_armv7l.whl size=398431 sha256=cf274fcbb0165313f4361ef79180cbb04cd035fdfa6efe9d146f57661b5b6645
  Stored in directory: /home/root/.cache/pip/wheels/2e/18/23/10f816dc1f57d13a6c8d6525d442e74d82e772bb60f864afbe
Successfully built cffi
Installing collected packages: sortedcontainers, pytz, typing-extensions, six, pycparser, aiofiles, python-dateutil, cffi, aiosqlite, cryptography, pyopenssl, asyncua
Successfully installed aiofiles-24.1.0 aiosqlite-0.21.0 asyncua-1.1.6 cffi-1.17.1 cryptography-45.0.4 pycparser-2.22 pyopenssl-25.1.0 python-dateutil-2.9.0.post0 pytz-2025.2 six-1.17.0 sortedcontainers-2.4.0 typing-extensions-4.14.0
(desk) root@bora:~#
  • bcrypt (password hashing)
(desk) root@bora:~# pip3 install bcrypt
Collecting bcrypt
  Downloading bcrypt-4.3.0-cp39-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl.metadata (10 kB)
Downloading bcrypt-4.3.0-cp39-abi3-manylinux_2_28_armv7l.manylinux_2_31_armv7l.whl (297 kB)
Installing collected packages: bcrypt
Successfully installed bcrypt-4.3.0
(desk) root@bora:~#
  • can (color animator)
(desk) root@bora:~# pip3 install can
Collecting can
  Downloading can-0.0.0.tar.gz (1.0 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: can
  DEPRECATION: Building 'can' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possible replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'can'. Discussion can be found at https://github.com/pypa/pip/issues/6334
  Building wheel for can (setup.py) ... done
  Created wheel for can: filename=can-0.0.0-py3-none-any.whl size=1318 sha256=e7f58a8a8fc193ebb2746d3c10f134fbda0b95a923d35110ce0eb6fd02ec96a7
  Stored in directory: /home/root/.cache/pip/wheels/29/98/17/410e0373425d200f16329ed1f2e57aba1cf9cb55f4058965ee
Successfully built can
Installing collected packages: can
Successfully installed can-0.0.0
  • cbor (Concise Binary Object Representation - CBOR is comparable to JSON, has a superset of JSON’s ability)
(desk) root@bora:~# pip3 install cbor
Collecting cbor
  Downloading cbor-1.0.0.tar.gz (20 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: cbor
  DEPRECATION: Building 'cbor' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possible replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'cbor'. Discussion can be found at https://github.com/pypa/pip/issues/6334
  Building wheel for cbor (setup.py) ... done
  Created wheel for cbor: filename=cbor-1.0.0-cp312-cp312-linux_armv7l.whl size=53125 sha256=4a847347f21d191174583bb2b9b4cbb54963e51886e9ed3e638d08930c067dad
  Stored in directory: /home/root/.cache/pip/wheels/44/3e/21/a739cbcc331a1ab45c326d6edbdac6118de4402f6076e30ff1
Successfully built cbor
Installing collected packages: cbor
Successfully installed cbor-1.0.0
(desk) root@bora:~#
  • ntplib (Python NTP)
(desk) root@bora:~# pip3 install ntplib
Collecting ntplib
  Downloading ntplib-0.4.0-py2.py3-none-any.whl.metadata (1.6 kB)
Downloading ntplib-0.4.0-py2.py3-none-any.whl (6.8 kB)
Installing collected packages: ntplib
Successfully installed ntplib-0.4.0
(desk) root@bora:~#
  • pymodbus (Modbus stack protocol)
(desk) root@bora:~# pip3 install pymodbus
Collecting pymodbus
  Downloading pymodbus-3.9.2-py3-none-any.whl.metadata (15 kB)
Downloading pymodbus-3.9.2-py3-none-any.whl (165 kB)
Installing collected packages: pymodbus
Successfully installed pymodbus-3.9.2
(desk) root@bora:~#
  • pyserial (Serial Port extension)
(desk) root@bora:~# pip3 install pyserial
Collecting pyserial
  Downloading pyserial-3.5-py2.py3-none-any.whl.metadata (1.6 kB)
Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
Installing collected packages: pyserial
Successfully installed pyserial-3.5
  • python-daemon (Standard daemon process library)
(desk) root@bora:~# pip3 install python-daemon
Collecting python-daemon
  Downloading python_daemon-3.1.2-py3-none-any.whl.metadata (4.8 kB)
Collecting lockfile>=0.10 (from python-daemon)
  Downloading lockfile-0.12.2-py2.py3-none-any.whl.metadata (2.4 kB)
Downloading python_daemon-3.1.2-py3-none-any.whl (30 kB)
Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Installing collected packages: lockfile, python-daemon
Successfully installed lockfile-0.12.2 python-daemon-3.1.2
(desk) root@bora:~#
  • python-dateutil (Python extension dateutil)
(desk) root@bora:~# pip3 install python-dateutil
Requirement already satisfied: python-dateutil in ./desk/lib/python3.12/site-packages (2.9.0.post0)
Requirement already satisfied: six>=1.5 in ./desk/lib/python3.12/site-packages (from python-dateutil) (1.17.0)
(desk) root@bora:~#
  • python-prctl (C extension for system call)
(desk) root@bora:~# pip3 install python-prctl
Collecting python-prctl
  Downloading python-prctl-1.8.1.tar.gz (28 kB)
  Preparing metadata (setup.py) ... done
Building wheels for collected packages: python-prctl
  DEPRECATION: Building 'python-prctl' using the legacy setup.py bdist_wheel mechanism, which will be removed in a future version. pip 25.3 will enforce this behaviour change. A possible replacement is to use the standardized build interface by setting the `--use-pep517` option, (possibly combined with `--no-build-isolation`), or adding a `pyproject.toml` file to the source tree of 'python-prctl'. Discussion can be found at https://github.com/pypa/pip/issues/6334
  Building wheel for python-prctl (setup.py) ... done
  Created wheel for python-prctl: filename=python_prctl-1.8.1-cp312-cp312-linux_armv7l.whl size=25652 sha256=35146e57649db8bb19fea8b0657271b4b016bef41e487b8f20dd46051804b0c8
  Stored in directory: /home/root/.cache/pip/wheels/e0/69/8c/53597ab369f366137acd8f1b07c68e08cbb0ec8e8d62ab6961
Successfully built python-prctl
Installing collected packages: python-prctl
Successfully installed python-prctl-1.8.1
(desk) root@bora:~#
  • requests (Python HTTP)
(desk) root@bora:~# pip3 install requests
Collecting requests
  Downloading requests-2.32.4-py3-none-any.whl.metadata (4.9 kB)
Collecting charset_normalizer<4,>=2 (from requests)
  Downloading charset_normalizer-3.4.2-py3-none-any.whl.metadata (35 kB)
Collecting idna<4,>=2.5 (from requests)
  Downloading idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
  Downloading urllib3-2.4.0-py3-none-any.whl.metadata (6.5 kB)
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2025.4.26-py3-none-any.whl.metadata (2.5 kB)
Downloading requests-2.32.4-py3-none-any.whl (64 kB)
Downloading charset_normalizer-3.4.2-py3-none-any.whl (52 kB)
Downloading idna-3.10-py3-none-any.whl (70 kB)
Downloading urllib3-2.4.0-py3-none-any.whl (128 kB)
Downloading certifi-2025.4.26-py3-none-any.whl (159 kB)
Installing collected packages: urllib3, idna, charset_normalizer, certifi, requests
Successfully installed certifi-2025.4.26 charset_normalizer-3.4.2 idna-3.10 requests-2.32.4 urllib3-2.4.0
(desk) root@bora:~#

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

At the end, the package installed can be listed:

(desk) root@bora:~# pip3 list
Package            Version
------------------ -----------
aiofiles           24.1.0
aiosqlite          0.21.0
asyncua            1.1.6
bcrypt             4.3.0
can                0.0.0
cbor               1.0.0
certifi            2025.4.26
cffi               1.17.1
charset-normalizer 3.4.2
cryptography       45.0.4
idna               3.10
lockfile           0.12.2
netifaces          0.11.0
ntplib             0.4.0
pip                25.1.1
pycparser          2.22
pymodbus           3.9.2
pyOpenSSL          25.1.0
pyserial           3.5
python-daemon      3.1.2
python-dateutil    2.9.0.post0
python-prctl       1.8.1
pytz               2025.2
requests           2.32.4
setuptools         80.9.0
six                1.17.0
sortedcontainers   2.4.0
typing_extensions  4.14.0
urllib3            2.4.0
wheel              0.45.1

package import[edit | edit source]

The installed packages can be imported and used:

(desk) root@bora:~# python3
Python 3.12.6 (main, Sep  6 2024, 19:03:47) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncua
>>> import can
>>> import cbor
>>> import daemon
>>> import dateutil
>>> import netifaces
>>> import ntplib
>>> import pymodbus
>>> import prctl
>>> import serial
>>> import requests
>>>