Request Module Import Failing Topic is solved

Python and python framework

Moderator: leecollings

Post Reply
User avatar
Luxtux
Posts: 31
Joined: Monday 14 August 2017 15:16
Target OS: Linux
Domoticz version: 2021.1 β
Location: Luxembourg
Contact:

Request Module Import Failing

Post by Luxtux »

Good afternoon,

I'm trying to use python instead of lua in my latest automation script; so i can learn python. The entire example given when you select python is already generating errors. after some googling i figured out that the wrong module is imported. Now im trying to import the request module but eventough its installed on the system, domoticz keeps giving the error below.

Code: Select all

 2021-03-10 15:24:29.584 Error: EventSystem: Failed to execute python event script "Python Test"
2021-03-10 15:24:29.584 Error: EventSystem: Traceback (most recent call last):
2021-03-10 15:24:29.584 Error: EventSystem: File "<string>", line 5, in <module>
2021-03-10 15:24:29.584 Error: EventSystem: ModuleNotFoundError: No module named 'requests' 
when i check the system it clearly states that requests is installed:

Code: Select all

>sudo pip3 list
Package             Version
------------------- ---------
asn1crypto          0.24.0
certifi             2020.12.5
chardet             4.0.0
cryptography        2.6.1
distro-info         0.21
entrypoints         0.3
idna                2.10
keyring             17.1.1
keyrings.alt        3.1.1
pip                 18.1
pycrypto            2.6.1
PyGObject           3.30.4
python-apt          1.8.4.3
pyxdg               0.25
requests            2.25.1
SecretStorage       2.3.1
setuptools          40.8.0
six                 1.12.0
unattended-upgrades 0.1
urllib3             1.26.3
wheel               0.32.3
the simple code im trying to execute is

Code: Select all

import DomoticzEvents as DEVS
from datetime import datetime
import requests as REQ


HueIP = '192.168.100.50'
HueUsername = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

for name, value in DEVS.user_variables.items():
    if name == 'AlarmTime' :
        #DEVS.Log("Python: User-variable '{0}' has value: {1}".format(name, value))
        ts = int(value)
        test = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%dT%H:%M:%SZ')
        DEVS.Log(test)
        response = REQ.get("http://"+ HueIP +"/api/"+ HueUsername +"/schedules")
        
        DEVS.Log(response.status_code)
 
The server is a debian 10 x64 VM with python version 3.7.3 and Domoticz V2020.2 (build 13051).

Any suggestions?
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Request Module Import Failing

Post by waltervl »

Is your domoticz also running as root?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
Luxtux
Posts: 31
Joined: Monday 14 August 2017 15:16
Target OS: Linux
Domoticz version: 2021.1 β
Location: Luxembourg
Contact:

Re: Request Module Import Failing

Post by Luxtux »

waltervl wrote: Wednesday 10 March 2021 17:10 Is your domoticz also running as root?
No of course not. :) when i run the pip3 list command as the Domoticz user, i get the same list as when i run it as root.
User avatar
Luxtux
Posts: 31
Joined: Monday 14 August 2017 15:16
Target OS: Linux
Domoticz version: 2021.1 β
Location: Luxembourg
Contact:

Re: Request Module Import Failing

Post by Luxtux »

This might explain it. when i print out the paths used by python in domoticz its different from the command line.
These are the on the python command line as domoticz user :

Code: Select all

['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/domoticz/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/lib/python3/dist-packages']
and this is the result in domoticz:

Code: Select all

2021-03-10 19:05:49.469 /opt/domoticz/scripts/python/
2021-03-10 19:05:49.469 /usr/lib/python37.zip
2021-03-10 19:05:49.469 /usr/lib/python3.7
2021-03-10 19:05:49.469 /usr/lib/python3.7/lib-dynload
User avatar
Luxtux
Posts: 31
Joined: Monday 14 August 2017 15:16
Target OS: Linux
Domoticz version: 2021.1 β
Location: Luxembourg
Contact:

Re: Request Module Import Failing

Post by Luxtux »

this is how i fixed the issue for future reference.

Code: Select all

import DomoticzEvents as DEVS
from datetime import datetime

if '/home/domoticz/.local/lib/python3.7/site-packages' not in sys.path :
    DEVS.Log('Path not found')
    sys.path.append( '/home/domoticz/.local/lib/python3.7/site-packages' )
else:
    DEVS.Log('Pathfound')

import requests
uytterk
Posts: 1
Joined: Tuesday 06 April 2021 15:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Request Module Import Failing

Post by uytterk »

Thanks Luxtux for your solution.
I have been struggling for long with this issue.
I had to adapt the path to the settings on my system, but got it working.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest