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

**URL:** <https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599>\
**Category:** Help\
**Tags:** docker, python\
**Created:** [March 11, 2019, 8:01pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599 "2019-03-11T20:01:15Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [March 11, 2019, 8:01pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/1 "2019-03-11T20:01:15Z")

</div>

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 &quot;/mnt/FILENAME.py&quot;, line 7, in &lt;module&gt;\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

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [March 11, 2019, 8:56pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/2 "2019-03-11T20:56:41Z")

</div>

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](https://docs.docker.com/engine/reference/builder/#from) the Losant Edge Agent.

So essentially you’d create a new [Dockerfile](https://docs.docker.com/engine/reference/builder/) that looks something like this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [March 11, 2019, 9:11pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/3 "2019-03-11T21:11:55Z")

</div>

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?

 ![Slide_to_share_1](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/d/d3dcf9185bc41aff5736a0c01640ccac79421889.jpeg) ![Slide_to_share_2](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/5/55791b4d40cbc4056744194850fc5790e891e94c.jpeg)

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [March 12, 2019, 3:21am UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/4 "2019-03-12T03:21:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [March 12, 2019, 8:23am UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/5 "2019-03-12T08:23:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [March 12, 2019, 8:28pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/6 "2019-03-12T20:28:15Z")

</div>

Since our Edge Agent is implemented in Node.js, we use the [Office Node Docker Image](https://hub.docker.com/_/node/) as our base image. That image does have many versions of Python pre-installed:

 ![image](https://us1.discourse-cdn.com/flex015/uploads/getstructure/original/2X/1/1b642485b75c87e7858c205dbcd46ee815d58b0a.png)

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](https://docs.losant.com/workflows/data/run-executable/).

---

<div class="post-metadata">

**Author:** ![anaptfox](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/anaptfox/32/873_2.png) [@anaptfox](https://forums.losant.com/u/anaptfox)\
**Post date:** [March 26, 2019, 7:27pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/7 "2019-03-26T19:27:35Z")

</div>

Hey @Alexander_Kondrov,

I wrote and published this just for you:

> **[Extending the Losant Edge Agent](https://www.losant.com/blog/extending-the-losant-edge-agent)**
>
> Learn how to include custom dependencies or scripts in the same environment as the Losant Edge Agent.

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

---

<div class="post-metadata">

**Author:** ![Alexander\_Kondrov](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/alexander_kondrov/32/1392_2.png) [@Alexander\_Kondrov](https://forums.losant.com/u/Alexander_Kondrov)\
**Post date:** [March 26, 2019, 8:38pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/8 "2019-03-26T20:38:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jules\_Huguenin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/jules_huguenin/32/4031_2.png) [@Jules\_Huguenin](https://forums.losant.com/u/Jules_Huguenin)\
**Post date:** [May 6, 2022, 4:36am UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/9 "2022-05-06T04:36:54Z")

</div>

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

```auto
# 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

---

<div class="post-metadata">

**Author:** ![Brandon\_Cannaday](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.losant.com/brandon_cannaday/32/14_2.png) [@Brandon\_Cannaday](https://forums.losant.com/u/Brandon_Cannaday)\
**Post date:** [May 6, 2022, 2:21pm UTC](https://forums.losant.com/t/python-libraries-minimalmodbus-on-edge-device-in-run-executable-node/1599/10 "2022-05-06T14:21:46Z")

</div>

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.

```auto
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.
