Page 1 of 1
Debugging a Python plugin
Posted: Saturday 30 December 2017 13:43
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!
Re: Debugging a Python plugin
Posted: Saturday 30 December 2017 17:08
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!?
Re: Debugging a Python plugin
Posted: Monday 01 January 2018 5:52
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.
Re: Debugging a Python plugin
Posted: Monday 05 February 2018 22:30
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

Re: Debugging a Python plugin
Posted: Friday 22 February 2019 21:01
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
Re: Debugging a Python plugin
Posted: Saturday 02 March 2019 14:54
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.
Re: Debugging a Python plugin
Posted: Sunday 03 March 2019 11:28
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.
Re: Debugging a Python plugin
Posted: Sunday 03 March 2019 21:57
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!