Difference between revisions of "DESK-MX6UL-AN-0004: Using Python for Embedded applications"

From DAVE Developer's Wiki
Jump to: navigation, search
(Virtual environments)
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
{{AppliesToAXEL ULite AN}}
 
{{AppliesToAXEL ULite AN}}
 
{{AppliesTo_SBC_Lynx_AN}}
 
{{AppliesTo_SBC_Lynx_AN}}
{{AppliesTo_RIALTO_SBC_AN}}
 
 
{{InfoBoxBottom}}
 
{{InfoBoxBottom}}
  
Line 19: Line 18:
 
|-
 
|-
 
| 2.0.0
 
| 2.0.0
| Apr 2022
+
| --- 2022
 
|[[DESK-MX6UL-L/General/Release_Notes#DESK-MX6UL-L_3.0.0|DESK-MX6UL-L 3.0.0]]
 
|[[DESK-MX6UL-L/General/Release_Notes#DESK-MX6UL-L_3.0.0|DESK-MX6UL-L 3.0.0]]
 
|-
 
|-
Line 83: Line 82:
 
As explained on [https://docs.python.org/3/tutorial/venv.html 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"''
 
As explained on [https://docs.python.org/3/tutorial/venv.html 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).
+
This avoids the problem related to Applications that sometimes need a specific version of a library.
  
 
* for example, create the <code>virtual environment</code> named '''desk'''
 
* for example, create the <code>virtual environment</code> named '''desk'''
Line 114: Line 113:
 
</pre>
 
</pre>
  
* <code>setuptools</code> can be upgraded too:
+
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).
 +
 
 +
After pip3 installation, the available (base) packages are:
  
 
<pre class="board-terminal">
 
<pre class="board-terminal">
(desk-env) root@desk-mx6ul-lynx:~# pip3 install --upgrade setuptools
+
root@desk-mx6ul-lynx:~# pip3 list
Requirement already satisfied: setuptools in ./desk-env/lib/python3.9/site-packages (49.2.1)
+
Package    Version
Collecting setuptools
+
---------- --------------
  Using cached setuptools-60.5.0-py3-none-any.whl (958 kB)
+
btrfsutil  5.10.1
Installing collected packages: setuptools
+
gpg        1.15.1-unknown
  Attempting uninstall: setuptools
+
iniparse   0.4
    Found existing installation: setuptools 49.2.1
+
libcomps   0.1.15
    Uninstalling setuptools-49.2.1:
+
pip        21.3.1
      Successfully uninstalled setuptools-49.2.1
+
PyGObject  3.38.0
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
+
setuptools 60.5.0
   distutils: /home/root/desk-env/lib/python3.9/site-packages
+
six        1.15.0
  sysconfig: /usr/lib/python3.9/site-packages
+
wheel      0.37.1
  WARNING: Value for scheme.purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
+
root@desk-mx6ul-lynx:~#  
  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 setuptools-60.5.0
 
(desk-env) root@desk-mx6ul-lynx:~#  
 
 
</pre>
 
</pre>
  
Line 171: Line 163:
 
Collecting pyserial
 
Collecting pyserial
 
   Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
 
   Downloading pyserial-3.5-py2.py3-none-any.whl (90 kB)
    Collecting fastapi
+
     |################################| 90 kB 780 kB/s             
  Downloading fastapi-0.72.0-py3-none-any.whl (52 kB)
 
     |████████████████████████████████| 52 kB 77 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.1 MB/s
 
Collecting starlette==0.17.1
 
  Downloading starlette-0.17.1-py3-none-any.whl (58 kB)
 
    |████████████████████████████████| 58 kB 619 kB/s
 
Collecting anyio<4,>=3.0.0
 
  Downloading anyio-3.5.0-py3-none-any.whl (79 kB)
 
    |████████████████████████████████| 79 kB 797 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)
 
    |████████████████████████████████| 90 kB 780 kB/s             
 
 
Installing collected packages: pyserial
 
Installing collected packages: pyserial
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
Line 203: Line 177:
 
   prefix = None
 
   prefix = None
 
Successfully installed pyserial-3.5
 
Successfully installed pyserial-3.5
(desk-env) root@desk-mx6ul-lynx:~# pip3 install pymodbus
+
(desk-env) root@desk-mx6ul-lynx:~# pip3 install modbus
 
Collecting pymodbus
 
Collecting pymodbus
 
   Downloading pymodbus-2.5.3-py2.py3-none-any.whl (154 kB)
 
   Downloading pymodbus-2.5.3-py2.py3-none-any.whl (154 kB)
     |████████████████████████████████| 154 kB 1.5 MB/s             
+
     |#############################| 154 kB 1.5 MB/s             
 
Collecting six>=1.15.0
 
Collecting six>=1.15.0
 
   Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
 
   Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Line 223: Line 197:
 
   prefix = None
 
   prefix = None
 
Successfully installed pymodbus-2.5.3 six-1.16.0
 
Successfully installed pymodbus-2.5.3 six-1.16.0
(desk-env) root@desk-mx6ul-lynx:~# pip3 install flask
 
Collecting flask
 
  Downloading Flask-2.0.2-py3-none-any.whl (95 kB)
 
    |████████████████████████████████| 95 kB 422 kB/s
 
Collecting Werkzeug>=2.0
 
  Downloading Werkzeug-2.0.2-py3-none-any.whl (288 kB)
 
    |████████████████████████████████| 288 kB 2.1 MB/s
 
Collecting itsdangerous>=2.0
 
  Downloading itsdangerous-2.0.1-py3-none-any.whl (18 kB)
 
Collecting click>=7.1.2
 
  Downloading click-8.0.3-py3-none-any.whl (97 kB)
 
    |████████████████████████████████| 97 kB 226 kB/s
 
Collecting Jinja2>=3.0
 
  Downloading Jinja2-3.0.3-py3-none-any.whl (133 kB)
 
    |████████████████████████████████| 133 kB 2.1 MB/s
 
Collecting MarkupSafe>=2.0
 
  Downloading MarkupSafe-2.0.1.tar.gz (18 kB)
 
  Preparing metadata (setup.py) ... done
 
Building wheels for collected packages: MarkupSafe
 
  Building wheel for MarkupSafe (setup.py) ... done
 
  Created wheel for MarkupSafe: filename=MarkupSafe-2.0.1-cp39-cp39-linux_armv7l.whl size=25720 sha256=4fa273af41ed51d0614beb76603162148fd15472aa0eb8c1c28a9c18ed137031
 
  Stored in directory: /home/root/.cache/pip/wheels/9f/6d/c8/1f59b07cf85ae842908006ec28f4477f7e4578df72c3eb0e46
 
Successfully built MarkupSafe
 
Installing collected packages: MarkupSafe, Werkzeug, Jinja2, itsdangerous, click, flask
 
  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 Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.2 click-8.0.3 flask-2.0.2 itsdangerous-2.0.1
 
 
(desk-env) root@desk-mx6ul-lynx:~# pip3 install fastapi
 
(desk-env) root@desk-mx6ul-lynx:~# pip3 install fastapi
 
Collecting fastapi
 
Collecting fastapi
 
   Downloading fastapi-0.72.0-py3-none-any.whl (52 kB)
 
   Downloading fastapi-0.72.0-py3-none-any.whl (52 kB)
     |████████████████████████████████| 52 kB 77 kB/s
+
     |################################| 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
 
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)
 
   Downloading pydantic-1.9.0-py3-none-any.whl (140 kB)
     |████████████████████████████████| 140 kB 2.1 MB/s
+
     |############################| 140 kB 2.3 MB/s          
Collecting starlette==0.17.1
 
  Downloading starlette-0.17.1-py3-none-any.whl (58 kB)
 
    |████████████████████████████████| 58 kB 619 kB/s
 
 
Collecting anyio<4,>=3.0.0
 
Collecting anyio<4,>=3.0.0
 
   Downloading anyio-3.5.0-py3-none-any.whl (79 kB)
 
   Downloading anyio-3.5.0-py3-none-any.whl (79 kB)
     |████████████████████████████████| 79 kB 797 kB/s
+
     |###############################| 79 kB 729 kB/s          
 
Collecting typing-extensions>=3.7.4.3
 
Collecting typing-extensions>=3.7.4.3
 
   Downloading typing_extensions-4.0.1-py3-none-any.whl (22 kB)
 
   Downloading typing_extensions-4.0.1-py3-none-any.whl (22 kB)
Line 278: Line 216:
 
Collecting idna>=2.8
 
Collecting idna>=2.8
 
   Downloading idna-3.3-py3-none-any.whl (61 kB)
 
   Downloading idna-3.3-py3-none-any.whl (61 kB)
     |████████████████████████████████| 61 kB 770 kB/s
+
     |###############################| 61 kB 805 kB/s          
 
Installing collected packages: sniffio, idna, typing-extensions, anyio, starlette, pydantic, fastapi
 
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>
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
Line 295: Line 233:
 
Collecting bottle
 
Collecting bottle
 
   Downloading bottle-0.12.19-py3-none-any.whl (89 kB)
 
   Downloading bottle-0.12.19-py3-none-any.whl (89 kB)
     |████████████████████████████████| 89 kB 724 kB/s
+
     |################################| 89 kB 688 kB/s          
 
Installing collected packages: bottle
 
Installing collected packages: bottle
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
Line 312: Line 250:
 
Collecting requests
 
Collecting requests
 
   Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
 
   Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 141 kB/s
+
     |##############################| 63 kB 124 kB/s          
Requirement already satisfied: idna<4,>=2.5 in ./desk-env/lib/python3.9/site-packages (from requests) (3.3)
 
 
Collecting charset-normalizer~=2.0.0
 
Collecting charset-normalizer~=2.0.0
 
   Downloading charset_normalizer-2.0.10-py3-none-any.whl (39 kB)
 
   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
 
Collecting certifi>=2017.4.17
 
   Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
 
   Downloading certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
     |████████████████████████████████| 149 kB 2.2 MB/s
+
     |###########################| 149 kB 547 kB/s          
Collecting urllib3<1.27,>=1.21.1
 
  Downloading urllib3-1.26.8-py2.py3-none-any.whl (138 kB)
 
    |████████████████████████████████| 138 kB 2.2 MB/s
 
 
Installing collected packages: urllib3, charset-normalizer, certifi, requests
 
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>
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
Line 372: Line 310:
 
Collecting scapy
 
Collecting scapy
 
   Downloading scapy-2.4.5.tar.gz (1.1 MB)
 
   Downloading scapy-2.4.5.tar.gz (1.1 MB)
     |████████████████████████████████| 1.1 MB 1.4 MB/s
+
     |#########| 1.1 MB 1.3 MB/s          
   Preparing metadata (setup.py) ... done
+
   Preparing metadata (setup.py) ... - \ done
Building wheels for collected packages: scapy
+
Using legacy 'setup.py install' for scapy, since package 'wheel' is not installed.
  Building wheel for scapy (setup.py) ... done
 
  Created wheel for scapy: filename=scapy-2.4.5-py2.py3-none-any.whl size=1261555 sha256=df65309a4f5e0ee8e9f5da16f5b22b41c5a3d0ab03d671f788451874deeca183
 
  Stored in directory: /home/root/.cache/pip/wheels/c8/9b/2f/012f0dbaf869afac8be52d4423f34eaa0b6c5c2d1292e40ebd
 
Successfully built scapy
 
 
Installing collected packages: scapy
 
Installing collected packages: scapy
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
 
   WARNING: Value for scheme.platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
Line 391: Line 325:
 
   root = None
 
   root = None
 
   prefix = None
 
   prefix = None
 +
    Running setup.py install for scapy ... / - \ | / done
 
Successfully installed scapy-2.4.5
 
Successfully installed scapy-2.4.5
 
(desk-env) root@desk-mx6ul-lynx:~# pip3 install nmap
 
(desk-env) root@desk-mx6ul-lynx:~# pip3 install nmap
Line 411: Line 346:
 
Collecting ujson
 
Collecting ujson
 
   Downloading ujson-5.1.0.tar.gz (7.1 MB)
 
   Downloading ujson-5.1.0.tar.gz (7.1 MB)
     |████████████████████████████████| 7.1 MB 10 kB/s
+
     |###############| 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>
 
   WARNING: Value for prefixed-purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
   distutils: /tmp/pip-build-env-y3_ib386/normal/lib/python3.9/site-packages
+
   distutils: /tmp/pip-build-env-aajn4vfi/normal/lib/python3.9/site-packages
 
   sysconfig: /usr/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>
 
   WARNING: Value for prefixed-platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
   distutils: /tmp/pip-build-env-y3_ib386/normal/lib/python3.9/site-packages
+
   distutils: /tmp/pip-build-env-aajn4vfi/normal/lib/python3.9/site-packages
 
   sysconfig: /usr/lib/python3.9/site-packages
 
   sysconfig: /usr/lib/python3.9/site-packages
 
   WARNING: Additional context:
 
   WARNING: Additional context:
Line 422: Line 357:
 
   home = None
 
   home = None
 
   root = None
 
   root = None
   prefix = '/tmp/pip-build-env-y3_ib386/normal'
+
   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>
 
   WARNING: Value for prefixed-purelib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
   distutils: /tmp/pip-build-env-y3_ib386/overlay/lib/python3.9/site-packages
+
   distutils: /tmp/pip-build-env-aajn4vfi/overlay/lib/python3.9/site-packages
 
   sysconfig: /usr/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>
 
   WARNING: Value for prefixed-platlib does not match. Please report this to <https://github.com/pypa/pip/issues/10151>
   distutils: /tmp/pip-build-env-y3_ib386/overlay/lib/python3.9/site-packages
+
   distutils: /tmp/pip-build-env-aajn4vfi/overlay/lib/python3.9/site-packages
 
   sysconfig: /usr/lib/python3.9/site-packages
 
   sysconfig: /usr/lib/python3.9/site-packages
 
   WARNING: Additional context:
 
   WARNING: Additional context:
Line 433: Line 368:
 
   home = None
 
   home = None
 
   root = None
 
   root = None
   prefix = '/tmp/pip-build-env-y3_ib386/overlay'
+
   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>
 
   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
 
   distutils: /home/root/desk-env/lib/python3.9/site-packages
Line 445: Line 380:
 
   distutils: /home/root/desk-env/lib/python3.9/site-packages
 
   distutils: /home/root/desk-env/lib/python3.9/site-packages
 
   sysconfig: /usr/lib/python3.9/site-packages
 
   sysconfig: /usr/lib/python3.9/site-packages
   Installing build dependencies ... done
+
   Installing build dependencies ... - \ | / - \ | / - done
 
   Getting requirements to build wheel ... done
 
   Getting requirements to build wheel ... done
   Preparing metadata (pyproject.toml) ... done
+
   Preparing metadata (pyproject.toml) ... - \ done
 
Building wheels for collected packages: ujson
 
Building wheels for collected packages: ujson
   Building wheel for ujson (pyproject.toml) ... done
+
   Building wheel for ujson (pyproject.toml) ... - \ | / - \ | done
   Created wheel for ujson: filename=ujson-5.1.0-cp39-cp39-linux_armv7l.whl size=37764 sha256=adb6cfae7a4edcd38706c5b910adeee4549af56433bbe17eed89b08f77dd25aa
+
   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
 
   Stored in directory: /home/root/.cache/pip/wheels/5b/11/cb/2e1acde83fd78adc6581984c55442e63d7595711b0b62d8110
 
Successfully built ujson
 
Successfully built ujson
Line 466: Line 401:
 
   prefix = None
 
   prefix = None
 
Successfully installed ujson-5.1.0
 
Successfully installed ujson-5.1.0
(desk-env) root@desk-mx6ul-lynx:~#
+
(desk-env) root@desk-mx6ul-lynx:~#  
 
</pre>
 
</pre>
 
=== check for installed packages and version ===
 
=== check for installed packages and version ===
Line 493: Line 428:
 
scapy              2.4.5
 
scapy              2.4.5
 
schema            0.7.5
 
schema            0.7.5
setuptools        60.5.0
+
setuptools        49.2.1
 
six                1.16.0
 
six                1.16.0
 
sniffio            1.2.0
 
sniffio            1.2.0
Line 526: Line 461:
 
>>> import requests
 
>>> import requests
 
>>> import flask
 
>>> import flask
>>> import fastapi
 
 
>>> import bottle
 
>>> import bottle
 
>>> import ujson
 
>>> import ujson
 +
>>> import fastapi
 
>>>  
 
>>>  
 
(desk-env) root@desk-mx6ul-lynx:~#  
 
(desk-env) root@desk-mx6ul-lynx:~#  
 
</pre>
 
</pre>

Revision as of 16:10, 20 January 2022

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
  • check and update pip3 in the the virtual environment
(desk-env) root@desk-mx6ul-lynx:~# which pip3
/home/root/desk-env/bin/pip3
(desk-env) root@desk-mx6ul-lynx:~# 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:~# 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).

After pip3 installation, the available (base) packages are:

root@desk-mx6ul-lynx:~# pip3 list
Package    Version
---------- --------------
btrfsutil  5.10.1
gpg        1.15.1-unknown
iniparse   0.4
libcomps   0.1.15
pip        21.3.1
PyGObject  3.38.0
setuptools 60.5.0
six        1.15.0
wheel      0.37.1
root@desk-mx6ul-lynx:~# 

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

(desk-env) root@desk-mx6ul-lynx:~# pip3 install wheel
Collecting wheel
  Using cached wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel
  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 wheel-0.37.1
(desk-env) root@desk-mx6ul-lynx:~#

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:~#