ImportError: No module named 'homeassistant'

AppDaemon throws an error with this script

2019-01-30 21:39:41.332492 INFO AppDaemon Version 3.0.2 starting
2019-01-30 21:39:41.334233 INFO Configuration read from: /home/homeassistant/conf/appdaemon.yaml
2019-01-30 21:39:41.340127 INFO AppDaemon: Starting Apps
2019-01-30 21:39:41.342120 WARNING config_dir: /home exists, but is not writeable, owner: root
2019-01-30 21:39:41.350691 INFO AppDaemon: Loading Plugin HASS using class HassPlugin from module hassplugin
2019-01-30 21:39:42.178811 INFO AppDaemon: HASS: HASS Plugin Initializing
2019-01-30 21:39:42.180380 INFO AppDaemon: HASS: HASS Plugin initialization complete
2019-01-30 21:39:42.181903 INFO Dashboards are disabled
2019-01-30 21:39:42.183552 INFO API is disabled
2019-01-30 21:39:42.200008 INFO AppDaemon: HASS: Connected to Home Assistant 0.86.3
2019-01-30 21:39:42.253672 INFO AppDaemon: Got initial state from namespace default
2019-01-30 21:39:44.393396 INFO AppDaemon: Reading config
2019-01-30 21:39:44.402105 INFO AppDaemon: /home/homeassistant/conf/apps/apps.yaml added or modified
2019-01-30 21:39:44.402645 INFO AppDaemon: /home/homeassistant/conf/apps/apps.yaml added or modified
2019-01-30 21:39:44.403116 INFO AppDaemon: App 'kodi' added
2019-01-30 21:39:44.403465 INFO AppDaemon: App 'hello_world' added
2019-01-30 21:39:44.404003 INFO AppDaemon: Adding /home/homeassistant/conf/apps to module import path
2019-01-30 21:39:44.406009 INFO AppDaemon: Loading App Module: /home/homeassistant/conf/apps/kodi.py
2019-01-30 21:39:44.427344 WARNING AppDaemon: ------------------------------------------------------------
2019-01-30 21:39:44.427850 WARNING AppDaemon: Unexpected error loading module: /home/homeassistant/conf/apps/kodi.py:
2019-01-30 21:39:44.428252 WARNING AppDaemon: ------------------------------------------------------------
2019-01-30 21:39:44.432961 WARNING AppDaemon: Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/appdaemon/appdaemon.py", line 2026, in check_app_updates
    self.read_app(mod["name"], mod["reload"])
  File "/usr/local/lib/python3.5/dist-packages/appdaemon/appdaemon.py", line 1809, in read_app
    self.modules[module_name] = importlib.import_module(module_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 673, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/homeassistant/conf/apps/kodi.py", line 3, in <module>
    from homeassistant.components.media_player.kodi import (
ImportError: No module named 'homeassistant'

2019-01-30 21:39:44.433542 WARNING AppDaemon: ------------------------------------------------------------
2019-01-30 21:39:44.433978 WARNING AppDaemon: Removing associated apps:
2019-01-30 21:39:44.434373 WARNING AppDaemon: kodi
2019-01-30 21:39:44.434843 INFO AppDaemon: Loading App Module: /home/homeassistant/conf/apps/hello.py
2019-01-30 21:39:44.438074 INFO AppDaemon: Initializing app hello_world using class HelloWorld from module hello
2019-01-30 21:39:44.659882 INFO hello_world: Hello from AppDaemon
2019-01-30 21:39:44.663363 INFO hello_world: You are now ready to run Apps!
2019-01-30 21:39:44.665367 INFO AppDaemon: App initialization complete

I start appDaemon with this commands:

sudo -u homeassistant -H -s
source /srv/homeassistant/bin/activate
appdaemon -c /home/homeassistant/conf

In the conf folder i have the appdaemon.yml:

appdaemon:
  threads: 10
  plugins:
    HASS:
      type: hass
      ha_url: http://192.168.178.68:8123
      token: xxxxx

And also in the conf folder is the apps folder with the kodi.py and the apps.yml. I copied the kodi.py from the link and didn’t change it. The apps.yml is this:

hello_world:
  module: hello
  class: HelloWorld
kodi:
  module: kodi
  class: DynamicKodiInputSelect

Does anybody know how i could fix this?

Are you sure this is the correct virtual environment for your appdaemon setup? It is normal to create a different venv for AD.

No i’m really not sure about that. I testet it also without it like in the instructions here but got the same error.

I wrote this a while ago to create a separate virtual environment for AD

And are you sure that script is compatible with AD version 3.0.2. I’m not certain, but I think it has an old style import header. You should compare it with the latest examples to check.

1 Like

the app that is used there is created for appdaemon 2.0
so it wont work without changing the code (and because its old and not suppoerted, its not even sure it would work at all)

it also seems to connect directly into homeassistant, that means that it is expected that appdaemon is installed in a specific place which they dont describe.
and for sure appdaemon and homeassistant must be installed in the same venv, to get this working, which is absolutely unadvisable.

edit: i made a PR to remove the app from the HA documents.

Alright thanks a lot for the quick help.
At least now i know i didn’t make anything wrong.

1 Like

the app can be rewritten to use it the appdaemon way though, without directly gettin the inf from HA.
but it needs a bit of reprogramming.

Yes for sure. But I just installed appdaemon and i’m also not really into python. So this will be a task for another time. I just thought that it would be a good opportunity to get a look into appdaemon and become something usefull at the same time. For now I will look to start with something that actually work :sweat_smile:

1 Like

in that case i advise appdaemon for beginner:

1 Like

In that case thanks again! I will definitely look into it.

1 Like