I don’t actually, I just tried it. I set the log level for python script up to debug but the only log I see is this:
2021-05-20 13:44:59 INFO (SyncWorker_26) [homeassistant.components.python_script] Executing test1.py: {}
I don’t see any errors like you’re showing.
I know python scripts execute in a sandboxed environment which prevent the use of any import statements. It looks like that’s what your script is hitting? But its confusing because there’s no import statement at that spot, its like its trying to import datetime when you go to use it and then failing.
The weird thing is it worked when I upgraded from 2021.4.6 to 2021.5.1. Today I upgraded to 2021.5.5 and then that happened.
So I downgraded back to 2021.5.1 and it still generated the same error?!? I downgraded yet again to 2021.4.6 but the error remained.
None of that makes sense to me because I am using Home Assistant Supervised so its operating environment, including its python interpreter, is self-contained (i.e. docker container). Reverting to 2021.5.1 should have put everything back to the way when it worked but it didn’t. I can’t think of any external configuration that can influence the python_script integration across versions.
I am now back to 2021.5.5 and at a loss to understand what is causing this fresh hell. The datetime module ought to be baked into the python_script integration so references to it should not cause errors about __import__ and “restricted_globals”.
FWIW, I opened an Issue in the Core repo but I think it will get a very low priority. Until this is resolved, a python_script I have used for months remains dead in the water.
I upgraded a second instance of Home Assistant, also running Home Assistant Supervised on Debian 10 (but its an RPI instead of a generic PC) and executing the python_script doesn’t produce the error.
So it’s not reproducible on the second instance, nor by CentralCommand, thereby making it all the more challenging to resolve on the first instance!
I’ve gathered a lot of information to characterize the problem but, unfortunately, nothing to solve it yet.
The problem persists regardless of the version I have tried. It even persists if I uninstall/re-install the python_script integration.
(I uninstalled the python_script integration, and deleted its related directory, upgraded to 2021.5.5, recreated the directory and re-installed the python_script integration and … the error message persists.)
This is sufficient python code to produce the error message:
Its the first line’s use of strptime that causes the error KeyError: '__import__'.
Error executing script: '__import__'
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 221, in execute
exec(compiled.code, restricted_globals)
File "test1.py", line 1, in <module>
KeyError: '__import__'
In contrast, the second line, the one using strftime, causes the same error message on my second instance of Home Assistant. However, there is precedent for strftime causing an error:
And as far back as 2017 where it even surprised balloob:
The odd thing is that my first instance of Home Assistant accepts neither strptime or strftime whereas my second instance accepts strptime but not strftime. How is this even possible when both use the same version of Home Assistant?
Something is wrong with the python_script integration because strftime shouldn’t be rejected (it’s commonly used and is definitely “safe”). Plus one of my two Home Assistant instances rejects both strftime and strptime. This kind of variability shouldn’t be possible.
As expected, strftime appears to be universally unacceptable to the python_script integration.
I have the bad luck of having a python_script integration that also dislikes strptime.
The ability of python_script integration to behave differently on two instances is incomprehensible to me. I don’t know what external factors could influence its behavior.
yes, and re-reading those links you posted, if you desperately need the strftime, you could use AppDaemon as Burningstone suggests.
Besides that, it really feels like a bug, this should not be happening. Did you file an issue yet?
btw mere dt = datetime.datetime.now() returns the same
In this case, I don’t need strftime, I need strptime.
If you are still using the code discussed in the “Python_script for countdown to birthdays/etc” thread, then you have this line somewhere in your system:
If you were experiencing the problem I am currently experiencing, that line would fail. Without strptime, converting date strings to datetime objects becomes messy.
BTW, this happened to me ‘out of the blue’. After upgrading to 2021.5.5, I noticed a sensor, normally generated by a python_script, was missing. I tracked the problem down to strptime now no longer acceptable within the sandboxed environment. If this can happen to me without any apparent reason, it can happen to someone else too.
I opened an Issue and posted lots of details but there has been no response yet. My impression is that the python_script integration has a low priority.
Just to double-check it’s not something gone wrong with your installation, can you fire up a shell within the system? Don’t know how Supervised works, but on my DSM Docker setup, I can run a bash terminal in the HA container and fire off this:
That’s not doing a date string to datetime object conversion. Your dateYear,dateMonth,dateDay variables already have the desired integer values. Anyway, I don’t want to go off-topic with this; the issue here is that strptime is normally available and I just found a case where it isn’t.
I get the same result but I don’t believe it sheds any light on the problem because the python_script integration employs RestrictedPython. Basically python3 cut down to only “safe” things (where the development team can decide what’s considered safe or not for use within Home Assistant).
Understood — just wanted to rule out something like a disk error on the base Python installation, which as I understand it, RestrictedPython just provides a filter to.