Wiki
Clone wikiPyRTEdoc / Install a Python module
The ImportError cannot be more annoying during development,
root@fx30s:~# python3 Python 3.5.2 (default, Oct 2 2020, 02:54:47) [GCC 6.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import PythonModuleINeedUrgently Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'PythonModuleINeedUrgently' >>>
Thanks to the beloved Python community, we have many modules around and most of them can be easily installed via pip. Unfortunately, when it comes to Yocto world, the focus is to keep the core image size small, and mostly you may have to rebuild the image to include a Python module. And often, the partition set up just does not have enough space to accommodate the new image which is far from ideal.
However, if you have installed PyRTE Legato app, you might be able to install the module you need without rebuilding the Linux. Here is an example to install PyModbus
Download the source code
Most Python modules are open source and you can get the source code from their repository. For PyModbus, we can download the compressed source code here, https://github.com/riptideio/pymodbus/releases/tag/v2.5.2
Decompressed the code, you would be able to see the module is fully written in Python, and the code directory is like this, pymodbus-2.5.2\pymodbus-2.5.2\pymodbus
Copy the code into device
ssh in FX30, create myapp directory if not yet, and in myapp, create another directory called site-packages,
root@fx30s:~/myapp/site-packages# pwd
/home/root/myapp/site-packages
Use SCP or WinSCP to copy the pymodbus folder into site-packages, then you should have the below on FX30,
root@fx30s:~/myapp/site-packages/pymodbus# ls -l -rw-r--r-- 1 root root 899 Jul 30 03:18 __init__.py -rw-r--r-- 1 root root 8596 Jul 30 03:18 bit_read_message.py -rw-r--r-- 1 root root 9205 Jul 30 03:18 bit_write_message.py drwxr-xr-x 3 root root 512 Jul 30 07:34 client -rw-r--r-- 1 root root 3440 Jul 30 03:18 compat.py -rw-r--r-- 1 root root 7485 Jul 30 03:18 constants.py drwxr-xr-x 3 root root 520 Jul 30 07:34 datastore -rw-r--r-- 1 root root 23243 Jul 30 03:18 device.py -rw-r--r-- 1 root root 29260 Jul 30 03:18 diag_message.py -rw-r--r-- 1 root root 6382 Jul 30 03:18 events.py -rw-r--r-- 1 root root 3691 Jul 30 03:18 exceptions.py -rw-r--r-- 1 root root 12413 Jul 30 03:18 factory.py -rw-r--r-- 1 root root 14237 Jul 30 03:18 file_message.py drwxr-xr-x 2 root root 608 Jul 30 07:34 framer -rw-r--r-- 1 root root 8363 Jul 30 03:18 interfaces.py drwxr-xr-x 2 root root 304 Jul 30 07:34 internal -rw-r--r-- 1 root root 7939 Jul 30 03:18 mei_message.py -rw-r--r-- 1 root root 14969 Jul 30 03:18 other_message.py -rw-r--r-- 1 root root 17008 Jul 30 03:18 payload.py -rw-r--r-- 1 root root 7966 Jul 30 03:18 pdu.py -rw-r--r-- 1 root root 12958 Jul 30 03:18 register_read_message.py -rw-r--r-- 1 root root 12058 Jul 30 03:18 register_write_message.py drwxr-xr-x 4 root root 432 Jul 30 07:34 repl drwxr-xr-x 3 root root 512 Jul 30 07:34 server -rw-r--r-- 1 root root 22806 Jul 30 03:18 transaction.py -rw-r--r-- 1 root root 7578 Jul 30 03:18 utilities.py -rw-r--r-- 1 root root 1550 Jul 30 03:18 version.py root@fx30s:~/myapp/site-packages/pymodbus#
Reboot the device, you should have your PyModbus ready to use.
root@fx30s:~# python3 Python 3.5.2 (default, Oct 2 2020, 02:54:47) [GCC 6.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from pymodbus.client.sync import ModbusTcpClient >>> client = ModbusTcpClient('127.0.0.1') >>> exit() root@fx30s:~#
Great, now you have installed the PyModbus module(hopefully in minutes). Just note, the module itself is fully written in Python which makes the installation more easier. Otherwise, there might be cross compiling and linking involved. We can explore this part further if there are demands.
Further readings,
- There are more explanation on how to use the PyModbus - PyModbus Homepage
- Refer to Hello World on how to boot up your code on power up or create a Python Legato app which should be even better.
- Use Requests to push data to some website.
Updated