Shell_command python scripts not executing in venv?

I’ve been working on a couple python scripts to run from HA, and i’m struggling with debugging. After a day of trial and error, I was able to determine that HASS was in fact calling my script, but it’s complaining about modules that don’t exist, even though I can successfully run the script from the venv command line.

here is my trial script

#!/usr/bin/env python3
try:
  import syslog
  import sys
  import requests
  import yaml
  secrets = yaml.load(open('/home/homeassistant/.homeassistant/configuration.yaml'))
  syslog.syslog(secrets['media_player'])
  syslog.syslog("CUSTOM COMMAND: Getting Garage Door Status")
except Exception as e:
  syslog.syslog("EXCEPTION: %s" % e)

running from the command line I’m ny venv, it executes and I get a syslog line as expected.

when I run it from HA, I get

/garagedoor.py: EXCEPTION: No module named 'yaml'

I use system to run HA, and it should be starting in the venv.

[Unit]
Description=Home Assistant
After=network.target

[Service]
Type=simple
User=homeassistant
ExecStartPre=source /srv/homeassistant/homeassistant_venv/bin/activate
ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c "/home/homeassistant/.homeassistant"

[Install]
WantedBy=multi-user.target

I start the service with

sudo service homeassistant start

As a last ditch effort, I stopped the system homeassistant instance ,and ran it from the venv command line

hass

Once I did that, my script ran fine.

Anyone have any ideas why I can’t get it to execute in the venv from the system daemon?

Thanks!

Is the HA user a member of sudoers? Remember that when you do a shell script in HA, it executes as the HA user.

no, it’s not. But, i’m trying to make the script execute as the HA user, so that part is working, it’s just not executing in the HA virtual environment.

hi @mconway!
Did you solve the problems? I have the same issue on my Berry and I cannot figure how to solve it :zipper_mouth_face:

Honestly, I don’t think I ever did. I switched over to running docker containers instead.