can't access python requests module

Python and python framework

Moderator: leecollings

Post Reply
AMelbye
Posts: 2
Joined: Thursday 17 September 2015 15:59
Target OS: Linux
Domoticz version:
Location: Norway
Contact:

can't access python requests module

Post by AMelbye »

Hi, I'll start with sofware versions:
Version: 2021.1
Build Hash: 8547c5b7e
Compile Date: 2021-04-17 17:29:11
dzVents Version: 3.1.7
Python Version: 3.9.2 (default, Mar 12 2021, 04:06:34) [GCC 10.2.1 20210110]
I'm on a raspberry pi running no GUI-version of (I think the the latest) raspbian

I'm making a script where I want to use the requests module to call an API.

However I can not get the module to load. I've now simplified down to this:

"#!/usr/lib/python3/dist-packages/" #I found advice in another thread about adding the path like this - did not work.
import requests


I get the error:
2022-01-11 16:19:00.185 Error: EventSystem: Failed to execute python event script "/home/pi/domoticz/scripts/python/script_time_sjekknoe.py"
2022-01-11 16:19:00.186 Error: EventSystem: Traceback (most recent call last):
2022-01-11 16:19:00.186 Error: EventSystem: File "/home/pi/domoticz/scripts/python/script_time_sjekknoe.py", line 2, in <module>
2022-01-11 16:19:00.186 Error: EventSystem: import requests
2022-01-11 16:19:00.186 Error: EventSystem: ModuleNotFoundError: No module named 'requests'



How to I tell domoticz to look for the module in the right place? It works fine when I run python in the console.

I've tried adding it with sys, but that doesn't seem to work either
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: can't access python requests module

Post by waltervl »

Try to install the request module with sudo as Domoticz will probably run as root.

There is currently a big issue with python plugins on bullseye. It is sidenote: known and a fix is close but until that there is no other way than wait, downgrade to Buster or use a docker environment.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
AMelbye
Posts: 2
Joined: Thursday 17 September 2015 15:59
Target OS: Linux
Domoticz version:
Location: Norway
Contact:

Re: can't access python requests module

Post by AMelbye »

@waltervl Thanks for your reply!

I actually just figured it out, I must have misspelled something the first time around. I wrote this code for my script and this problem is now solved:

Code: Select all

import DomoticzEvents as DE
import sys

if '/usr/lib/python3/dist-packages/' not in sys.path :  
    DE.Log('Path not found')
    sys.path.append('/usr/lib/python3/dist-packages/')
    DE.Log('Path added')
else:
    DE.Log('Path found')

import requests
Thanks for your reply though. I don't intend to use any plugins for now, I'm however working on a script to adjust my heaters based on electricity price, and I'm using python this time because I used it more recently and coding with it would be quite effortless if I didn't have to struggle with the implementation. My next problem is that writing to user variables doesn't seem to work... I might be able to do without it though as long as changing state of devices still works (I haven't checked)
PeJeWe
Posts: 56
Joined: Monday 28 November 2016 20:52
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: can't access python requests module

Post by PeJeWe »

waltervl wrote: Tuesday 11 January 2022 22:21 Try to install the request module with sudo as Domoticz will probably run as root.

There is currently a big issue with python plugins on bullseye. It is sidenote: known and a fix is close but until that there is no other way than wait, downgrade to Buster or use a docker environment.
Is there a fix on bullseye for this now, because i have the same problem with a import requests on a python script
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: can't access python requests module

Post by waltervl »

That is all fixed in 2022.2 and later.
There are still some issues when you define sub functions in your python script, see for example viewtopic.php?p=297430
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
PeJeWe
Posts: 56
Joined: Monday 28 November 2016 20:52
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: can't access python requests module

Post by PeJeWe »

waltervl wrote: Wednesday 12 April 2023 9:48 That is all fixed in 2022.2 and later.
There are still some issues when you define sub functions in your python script, see for example viewtopic.php?p=297430
i want to use a python script for my toon because when i let it run by DzVents i get random errors like "Toon has been running for more then 10 seconds".
So i want to use the following python script:

Code: Select all

import json
import os
import requests

ToonThermostat = 'Toon Thermostaat'
ToonTemperature = 'Temp Woonkamer'
ToonIP = '192.168.100.130'
DomoticzIP = '192.168.100.140:8080'

def update_temperatures():
    url = "http://{ToonIP}/happ_thermstat?action=getThermostatInfo"
    response = requests.get(url)
    if response.status_code == 200:
        thermostat_info = json.loads(response.content)
        current_setpoint = thermostat_info['currentSetpoint'] / 100
        current_temperature = thermostat_info['currentTemp'] / 100
        current_temperature = round(current_temperature, 1)

        if round(domoticz.devices(ToonThermostat).setPoint * 100) != round(current_setpoint * 100):
            domoticz.log('Update van de temperatuur instelling naar: ' + str(current_setpoint) + ' °C', domoticz.LOG_FORCE)
            domoticz.devices(ToonThermostat).updateSetPoint(current_setpoint)

        if round(domoticz.utils.round(domoticz.devices(ToonTemperature).temperature, 1)) != round(current_temperature, 1):
            domoticz.log('Update van de Kamertemparatuur : ' + str(current_temperature) + ' °C', domoticz.LOG_FORCE)
            domoticz.devices(ToonTemperature).updateTemperature(current_temperature)

def execute(domoticz):
    if debug:
        domoticz.log('DEBUG: Script uitvoeren', domoticz.LOG_INFO)
    update_temperatures()

    return {
        'on': {'timer': {'every minute'}},
        'logging': {'level': domoticz.LOG_ERROR, 'marker': 'scriptVar'},
        'execute': execute
    }
i have set the trigger to all
when i run this in domoticz in events ill get the following error:

Code: Select all

File "ToonPY", line 3, in <module>
Error: ModuleNotFoundError: No module named 'requests'
I run this on my rpi3b+ with bullseye

Code: Select all

Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (2.28.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests) (1.26.15)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.9/dist-packages (from requests) (2022.12.7)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.9/dist-packages (from requests) (3.1.0)
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: can't access python requests module

Post by waltervl »

Seems the same issue as discussed in the other topic I referred to.

Your Dzvents issue is probably easier to solve. Did you use the async way of calling openUrl()?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
PeJeWe
Posts: 56
Joined: Monday 28 November 2016 20:52
Target OS: Raspberry Pi / ODroid
Domoticz version: Latest
Location: Netherlands
Contact:

Re: can't access python requests module

Post by PeJeWe »

waltervl wrote: Wednesday 12 April 2023 23:43 Seems the same issue as discussed in the other topic I referred to.

Your Dzvents issue is probably easier to solve. Did you use the async way of calling openUrl()?
My coding skills are not that good to fully understand what you mean by "the async way of calling openUrl()"
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: can't access python requests module

Post by waltervl »

It is discussed in the Dzvents documentation: https://www.domoticz.com/wiki/DzVents:_ ... P_requests

The use of callback will make that a slow http connection will not stall your scripts.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest