custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
868 stars 46 forks source link

Shapely in pyscript fails to work as of HA 2022.7 #369

Closed wigster closed 2 years ago

wigster commented 2 years ago

Hi,

I have a pyscript app that uses the shapely library. This in turn depends on a package installed to the OS (I guess the HA container), so I have to get that installed using a shell script in HA (called on every HA start).

#!/bin/sh
apk update
apk add geos

After an upgrade to 2022.7(.3), shapely fails to work. I get the following error (this is from a hass kernel in jupyter)

Exception in <jupyter_0> line 1:
    from shapely.geometry import Polygon
    ^
OSError: Error loading shared library libgeos-09470e6b.so.3.10.2: No such file or directory (needed by /usr/local/lib/python3.10/site-packages/Shapely.libs/libgeos_c-55ebde92.so.1.16.0)

Reverting to 2022.6.7 fixes this problem. Not really sure where the problem is, but I guess it's some interaction with python 3.10. Not really sure whether pyscript is to blame honestly, but any pointers would be extremely helpful.

Thanks!

wigster commented 2 years ago

To be more precise, here is a minimal example with logs:

config.yaml

apps:
  tester:
    hello: yes

tester/__init__.yaml

log.warning("Tester: restarting...")

import shapely
from shapely.geometry import Polygon
from shapely.geometry import MultiPolygon
from shapely.ops import unary_union

log.warning("Tester: We got here!")

# startup trigger
@time_trigger('startup')
def startup():

    log.warning("Tester: We even got triggered.")

Like I was saying, this package needs the geos library installed on the OS side, so I do that with an automation triggered on startup:

- alias: "HA: Startup Patching of Docker container"
  id: "rouep9rtnpwer8yweboryg"
  trigger:
    - platform: homeassistant
      event: start
  action:
    - service: shell_command.patch_startup
  initial_state: on

and the corresponding shell script: scripts/patch_startup.sh

#!/bin/sh
apk update
apk add geos

Now for the logs. In HA 2022.6.7, if I do not install the geos library, I get the following error:

2022-07-15 15:54:25 WARNING (MainThread) [custom_components.pyscript.apps.tester] Tester: restarting...
2022-07-15 15:54:25 ERROR (MainThread) [custom_components.pyscript.apps.tester] Exception in </config/pyscript/apps/tester/__init__.py> line 4:
from shapely.geometry import Polygon
^
OSError: Could not find lib geos_c or load any of its variants ['libgeos_c.so.1', 'libgeos_c.so'].

And if I trigger the automation running the shell script manually and restart pyscript in dev tools, the "app" runs fine:

2022-07-15 15:28:03 WARNING (MainThread) [custom_components.pyscript.apps.tester] Tester: restarting...
2022-07-15 15:28:03 WARNING (MainThread) [custom_components.pyscript.apps.tester] Tester: We got here!
2022-07-15 15:28:03 WARNING (MainThread) [custom_components.pyscript.apps.tester.startup] Tester: We even got triggered.

while in 2022.7.5 it bombs out, with the same error (different to 2022.6.7) whether the automation is run or not.

2022-07-15 15:39:48 WARNING (MainThread) [custom_components.pyscript.apps.tester] Tester: restarting...
2022-07-15 15:39:48 ERROR (MainThread) [custom_components.pyscript.apps.tester] Exception in </config/pyscript/apps/tester/__init__.py> line 4:
from shapely.geometry import Polygon
^
OSError: Error loading shared library libgeos-09470e6b.so.3.10.2: No such file or directory (needed by /usr/local/lib/python3.10/site-packages/Shapely.libs/libgeos_c-55ebde92.so.1.16.0)

Very much appreciate any help.

wigster commented 2 years ago

It seems the problem is with the fact that there is no wheel in pipy for Shapely for aarch64 running under Alpine Linux (i.e. the standard HA OS install). Under HA <=2022.6, one could install the geos libraries into the container and what pip managed to install would work. For reasons beyond my knowledge, this is no longer happening under python 3.10 in 2022.7. As a kludge, it is possible to copy the libraries included in the Shapely package to /usr/local/lib, and things work despite the fact they are compiled with gcc. But I am not sure if this is safe.