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

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

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.

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?

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.

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?

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.

1 Like

Hey @Alexander_Kondrov,

I wrote and published this just for you:

It’s a guide on extending the edge agent. Let me know if you have any questions!

2 Likes

Hi Taron,
I really appreciate your article written specially for me! I took a look and I will get back with deeper reading later. I already figured out how to extend docker container for our needs, but your article will definitely save a few hours to people new to dockers. I will also use it as a guidance of best practice working with losant edge agent
Thank you,
Alexander

1 Like

Thanks @anaptfox for the tutorial.
I got stuck with the second example with myScript.py - I’m guessing this is the example script below?

# numpy is a custom dependency I need to install
import numpy as np

# assume this is really hard and complex math :) 
a = np.array([[1, 2, 3], [4, 5, 6]])

# this will go to sdtout in the workflow engine
# this will output (2/3)
print(a.shape)

I’m not sure I get where it should be saved on the device?
Also, what’s the role of the /opt/custom-scripts folder?

Thanks
Jules

Hey @Jules_Huguenin,

That tutorial does require a good understanding of how Docker works and how to build new Docker images.

I’m not sure I get where it should be saved on the device?

The file is getting built directly into the Docker image. Looking at the Dockerfile, the script needs to be in the same folder as the Dockerfile file when the image is built.

COPY ./myScript.py /opt/custom-scripts/

Also, what’s the role of the /opt/custom-scripts folder?

There’s nothing special about this folder. It’s just where the author chose to place scripts inside the image. The Docker build process will create this folder inside the image and copy the script to it.