Python Libraries (MinimalModbus) on Edge Device in Run Executable Node

docker
python

#1

Hello! I am starting my way with Edge Agent and trying to run an existing python script through Run Executable node. The python script includes the following libraries:

  • import os, sys
  • import minimalmodbus
  • import serial

As the output of the script I get an error:

"
Traceback (most recent call last):\n File "/mnt/FILENAME.py", line 7, in <module>\n import minimalmodbus\nImportError: No module named minimalmodbus\n"
"

Could you help me to figure out how to specify minimalmodbus library (module) for docker/agent/Run Executable Node to get rid of this error and get my script result published to Losant?

Thank you


#2

Since the Edge Agent runs in a container, it’s best to think of it as an entirely separate environment. This means dependencies like minimalmodbus are not automatically available inside the container, even if you’ve installed them in the host OS.

If you’re familiar with Docker, the best solution is to build a new container that’s extended FROM the Losant Edge Agent.

So essentially you’d create a new Dockerfile that looks something like this:

FROM losant/edge-agent:1.6.1

# Install additional dependencies
# COPY python scripts to container file system

How to install the dependencies varies greatly based on your specific python environment, but there are a lot of resources available online for how to do Python and Docker together.

This Dockerfile will then allow you to build a new image that includes anything you’ve added. The Run Executable Node can then run your custom python scripts directly from the container file system.


#3

Hi Brandon, Thank you for the prompt reply. I will try to learn how to / build an extended container. By the way, I tried to use a new Serial Node in parallel to read Modbus RTU Values. No much Help Docs available yet, but I generated (validated) Request bytes with Simply Modbus Master, then specified port (copied from my working python routine), specified number of bytes. Getting the following error in losant agent logs:
2019-03-11T20:51:10.353Z [warn] Error in serial manager while listening at /dev/ ttyAMA0: Error:Error: No such file or directory, cannot open /dev/ttyAMA0
2019-03-11T20:51:10.354Z [warn] Serial path will retry to open in 30 seconds.
2019-03-11T20:51:40.362Z [warn] Serial path /dev/ttyAMA0 is attempting to reopen

Could you advise if I forgot anything?


#4

Getting access to serial (or any device) inside Docker can certainly be a challenge. We’re writing up more detailed guides now.

At a high level you have to allow non-root users to read/write the device and run the container with the --privileged flag.

While you’re waiting on us to write up a guide, I’d recommend checking out some other online resources about accessing serial devices with Docker. There are some good tutorials out there.


#5

Brandon, Thank you for the prompt reply. I actually missed your answer, but was able to make my Serial and Modbus RTU working, reporting Device Values through Losant Agent to Losant Cloud. Still need to learn how to make extendable .dockable config file though. I got a result by using --privileged flag and manually installing pip, minimalmodbus, and pyserial modules. Any plans to support Python 3 by the way?


#6

Since our Edge Agent is implemented in Node.js, we use the Office Node Docker Image as our base image. That image does have many versions of Python pre-installed:

When you just invoke python, it’s symlinked to Python 2.7. If you want to run Python 3, you can invoke python3 myScript.py from the Run Executable Node.