Debugging a Python plugin

Python and python framework

Moderator: leecollings

Post Reply
galinette
Posts: 68
Joined: Monday 11 December 2017 22:57
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Debugging a Python plugin

Post by galinette »

Hi,

I am developping a Python plugin for a multi-channel electrical usage sensor (https://www.legrand.fr/pro/catalogue/31 ... -6-modules)

I developped it on a Windows sandbox domoticz installation and it works fine.

However, on my main Raspberry Pi installation, the plugin fails (and makes Domoticz unusable) with this error:

Code: Select all

2017-12-30 13:36:52.543 Error: (Legrand_412000) failed to load 'plugin.py', Python Path used was ':/usr/lib/python35.zip:/usr/lib/python3.5:/usr/lib/python3.5/plat-arm-linux-gnueabihf:/usr/lib/python3.5/lib-dynload'.
2017-12-30 13:36:52.543 Error: (Legrand) Module Import failed, exception: 'ImportError'
2017-12-30 13:36:52.543 Error: (Legrand) Module Import failed: ' Name: plugin'
2017-12-30 13:36:52.544 Error: (Legrand) Error Line details not available. 
There is no usable error message for understanding the causes. How may I get more verbose error log?

Thanks!
febalci
Posts: 331
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Debugging a Python plugin

Post by febalci »

Maybe it would be better for you to post your plugin.py. It seems like plugin.py itself is trying to import a module called plugin.py!?
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Debugging a Python plugin

Post by Dnpwwo »

@galinette,

During initial import of the plugin.py file Python is a little shy about saying what went wrong.

This can be worked around by putting all import statements (apart from 'import Domoticz') into the onStart function. Domoticz will then often be told the line number that failed.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
DenisMartin
Posts: 4
Joined: Monday 05 February 2018 22:26
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: Debugging a Python plugin

Post by DenisMartin »

Hi,

I've developped a c++ plugin for Legrand EcoCompteur (available on last beta).
It's not as flexible as python, but it's simpler for those who don't want to code :)
Patator100
Posts: 3
Joined: Monday 11 June 2018 23:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Debugging a Python plugin

Post by Patator100 »

Hi Galinette,

Have you finally managed to get your python plugin working ? I'm looking for a py plugin rather that C++ plugin for my ecocompteur (easier to modify and implement)


thanks for your feedback
patator
Samael
Posts: 27
Joined: Saturday 12 January 2019 11:48
Target OS: Windows
Domoticz version: 4.9701
Contact:

Re: Debugging a Python plugin

Post by Samael »

@Dnpwwo,

During dubugging a phyton plugin I found that it was very hard to diagnose 'import' related problems in embedded phyton..

You can add to any plugin.py (BaseTemplate.py, for ex.)

Code: Select all

import sys
import site
path=''
path=site.getsitepackages()
for i in path:
    sys.path.append(i)

import miio.philips_bulb
after that you will get

Code: Select all

2019-03-02 13:18:52.277 (test) Pushing 'InitializeMessage' on to queue
2019-03-02 13:18:52.312 (test) Processing 'InitializeMessage' message
2019-03-02 13:18:52.277 Status: (test) Started.
2019-03-02 13:18:52.348 Error: (BasePlug) failed to load 'plugin.py', Python Path used was 'C:\Program Files (x86)\Domoticz\plugins\test\;C:\Python\python36.zip;C:\Python\Lib\;C:\Python\DLLs\;C:\Python\Scripts\;C:\Python\Lib\site-packages\;C:\Program Files (x86)\Domoticz'.
2019-03-02 13:18:52.349 Error: (test) Module Import failed, exception: 'NameError'
2019-03-02 13:18:52.349 Error: (test) Error Line details not available.
Of course miio is installed and worked. If I use import inside onStart function it works only once but after that the whole Python subsytem is hang. So currently I use another py file where import is done and call it from plugin.py. If there are restrictions on the import of modules and packages, it is better to describe it on the page https://www.domoticz.com/wiki/Developin ... hon_plugin. If it is a bug I can create an issue.
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Debugging a Python plugin

Post by Dnpwwo »

@Samael,

From a quick look at the Miio import it seems to be multi-threaded (imports zeroconf which uses callbacks) which introduces some extra challenges for plugin developers. Have a look at https://www.domoticz.com/wiki/Developin ... n#Overview to see what I mean.

If Domoticz aborts when you Update the plugin (which sounds like what you are describing) it will most likely be due to threads that have not been shutdown whe the plugin stopped. Have a look at how to check if this is the problem here: https://github.com/domoticz/domoticz/bl ... hreaded.py or put this in onStop:

Code: Select all

        import threading
        for thread in threading.enumerate():
            if (thread.name != threading.current_thread().name):
                Domoticz.Log("'"+thread.name+"' is running, it must be shutdown otherwise Domoticz will abort on plugin exit.")
if any threads show up in the output then Domoticz is about to abort.

I would suggest you run a recent beta if you want to use this library, at the least, you won't need to worry about handling 'site' manually because that is now done for you.
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
Samael
Posts: 27
Joined: Saturday 12 January 2019 11:48
Target OS: Windows
Domoticz version: 4.9701
Contact:

Re: Debugging a Python plugin

Post by Samael »

Wow!

I tested on dev branch and now miio package fully works without problems. I placed import inside onStart and plugin works flawless. No hangs, no any threads in onStop.

Thanks!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest