Page 1 of 1
Running python scripts with external libraries
Posted: Wednesday 27 December 2017 20:58
by timberwolf
I have this strange situation that probably has a simple solution. I want to run the following script (script_time_alarm.py):
Code: Select all
import DomoticzEvents as DE
import verisure
session = verisure.Session('email', 'password')
session.login()
armstate = session.get_arm_state()
session.logout()
print(armstate["statusType"])
DE.Log("Alarm status")
The problem is that Domoticz will not run the script with the verisure library included. If i remove DomoticzEvents i can run it from the command line without problem. If i remove the Verisure library domoticz runs it as it should every minute.
What have i done wrong?
Re: Running python scripts with external libraries
Posted: Wednesday 27 December 2017 21:04
by febalci
You can import python modules in plugin framework but not in events as far as i know.
Re: Running python scripts with external libraries
Posted: Thursday 28 December 2017 13:22
by moroen
timberwolf wrote: ↑Wednesday 27 December 2017 20:58
I have this strange situation that probably has a simple solution. I want to run the following script (script_time_alarm.py):
Code: Select all
import DomoticzEvents as DE
import verisure
session = verisure.Session('email', 'password')
session.login()
armstate = session.get_arm_state()
session.logout()
print(armstate["statusType"])
DE.Log("Alarm status")
The problem is that Domoticz will not run the script with the verisure library included. If i remove DomoticzEvents i can run it from the command line without problem. If i remove the Verisure library domoticz runs it as it should every minute.
What have i done wrong?
The python-events struggles a bit with import-paths. Try saving the verisure module in the scripts/python-folder.
Regards,
M
Re: Running python scripts with external libraries
Posted: Wednesday 13 February 2019 13:27
by miroslavpetrov
Same problem here. The script works in the command line but when triggered with switch on action or from blockly event, the script doesn’t work. Is there a solution or I should integrate the external scripts in the main script?
Re: Running python scripts with external libraries
Posted: Wednesday 13 February 2019 17:52
by Itschi
I had the same problem and finally got it working with the following code:
Code: Select all
import sys
import importlib.util
MODULE_PATH = 'C:\Program Files (x86)\Python36-32\Lib\site-packages\modulename\__init__.py'
MODULE_NAME = 'modulename'
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = module
spec.loader.exec_module(module)
Regards, Itschi
Re: Running python scripts with external libraries
Posted: Saturday 21 August 2021 21:39
by Flopp
Have a look at my plugin, more feature will come but I need help to test them.
https://github.com/flopp999/Verisure-Domoticz