Running python scripts with external libraries

Python and python framework

Moderator: leecollings

Post Reply
timberwolf
Posts: 5
Joined: Wednesday 27 December 2017 9:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Running python scripts with external libraries

Post 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?
febalci
Posts: 331
Joined: Monday 03 July 2017 19:58
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Running python scripts with external libraries

Post by febalci »

You can import python modules in plugin framework but not in events as far as i know.
moroen
Posts: 408
Joined: Sunday 15 January 2017 11:06
Target OS: Linux
Domoticz version: beta
Location: Norway
Contact:

Re: Running python scripts with external libraries

Post 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
miroslavpetrov
Posts: 33
Joined: Wednesday 03 February 2016 13:35
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Running python scripts with external libraries

Post 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?
Automation Hardware:
Domoticz on Raspberry Pi 3 with HomeBridge.
Zigbee2MQTT + Xiaomi sensors
Fibaro HomeCenter 2 + Fibaro Dimmer2 & Double Switch2 + Aeon Multisensor 6 + Remotec ZXT-120
Eastron SDM230
Itschi
Posts: 18
Joined: Saturday 16 May 2015 14:58
Target OS: Windows
Domoticz version:
Contact:

Re: Running python scripts with external libraries

Post 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
Flopp
Posts: 279
Joined: Sunday 03 January 2016 14:55
Target OS: -
Domoticz version:
Location: Sweden
Contact:

Re: Running python scripts with external libraries

Post 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest