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).