Running Python Scripts with Imports using pyscript

Hello everyone - I am trying to run on my HA installation a Python script that is heavily based on Google’s Calendar API Example which requires a bunch of imports. According to the docs running this should be possible with pyscript and a requirements.txt, but I can’t find out how.

My HA installation is running on a Pi4, is fairly vanilla, mostly updated to latest. pyscript is installed from HACS with configuration steps in configuration.yaml done, the script, requirements.txt and the token.json file is located on HA /config/pyscript and I get this in the logs which shows what the problem is, as expected if the modules are not installed:

2022-10-02 11:47:27.585 ERROR (MainThread) [custom_components.pyscript.file.calendar] Exception in </config/pyscript/calendar.py> line 10:
from googleapiclient.discovery import build
^
ModuleNotFoundError: No module named ‘googleapiclient’

It seems that requirements.txt is not being read. It just has 3 lines like you’d expect -

google-api-python-client
google-auth-httplib2
google-auth-oauthlib

Can anyone help me out? I am proficient in Python but a relative novice in HA.

To answer my own question - The reason why the script wasn’t running is that pyscript does not like the if __name__ == '__main__' decorator. Just call the entry function. Simple, when you know :man_facepalming:

Then swiftly on to the next problem - trying to wrap the various blocking system calls to Google’s API and handling the authentication and tokens. After a few hours going in circles with the script throwing errors about wrapping, the suggestions in the docs not working I gave up.

After installing AppDaemon and spending a half hour everything is working as expected.