I’m trying to debug my custom Azure IoT Edge module in Python using Visual Studio Code, however when I run
client = IoTHubModuleClient.create_from_edge_environment()
I get the following error:
Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory: ''
I’m thinking this may be because I am missing a step in connecting to the Azure IoT Hub but I can’t identify what I might be missing. For reference, I am following the tutorials at https://learn.microsoft.com/en-us/azure/iot-edge/tutorial-python-module?view=iotedge-1.4 and https://learn.microsoft.com/en-us/azure/iot-edge/how-to-vs-code-develop-module?view=iotedge-1.4&tabs=csharp&pivots=iotedge-dev-cli.
I’m simply just trying to build a custom IoT Edge module in Python and debug it locally to confirm it’s working as expected. Thanks in advance for any suggestions.
2
Answers
For those still struggling, I was able to successfully run the module code locally by running the
Azure IoT Edge: Start IoT Edge Hub Simulator for Single Module
command from the command palette in Visual Studio Code.This resolved the FileNotFound error when executing the
client = IoTHubModuleClient.create_from_edge_environment()
line in my connect_client() method.Note The following approach has to be used to debug and test the Python modules that are deployed on the Azure IoT Edge device
I have done some extensive research and testing to debug the Azure IoT Edge modules remotely. Here is what worked for me. The approach to use ptvsd library for remote debugging has been deprecated. Instead, we can use debugpy to debug the python code remotely.
To enable remote debugging, open inbound TCP port 5678 on the VM where Azure IoT Edge modules are set up. Within the Python program add import debugpy and
debugpy.listen((‘0.0.0.0’,5678)) statements. Here is my Python code I have tested and deployed to the Azure IoT Edge Device.
Before building the docker image and deploying the Module, edit the Dockerfile.amd64.debug file to install debugpy on the IoT Edge device. Please refer below for the file content.
Here is the luanch.json file format for debugging the Python module remotely.
Sign into Docker by entering the following command in the terminal. Sign in with the username, password, and login server from your Azure container registry. You can retrieve these values from the Access keys section of your registry in the Azure portal.
In the VS Code explorer, right-click the deployment.debug.template.json file and select Build and Push IoT Edge Solution The build and push command starts three operations. First, it creates a new folder in the solution called config that holds the full deployment manifest, built out of information in the deployment template and other solution files. Second, it runs docker build to build the container image based on the appropriate dockerfile for your target architecture. Then, it runs docker push to push the image repository to your container registry.
You can then deploy the Module to the Azure IoT Edge device using the steps provided in the section Deploy your module. If you have used deployment.debug.template.json file, you would have deployment.debug.amd64 source file generated in the config folder.
Once the module is deployed and running, you can put a break point in the VS Code and start debugging your IoT Edge Node Module remotely. Please find the below image showing the breakpoint getting hit when I restart the Node Module on the Azure IoT Edge device from Azure portal.