Hassio/AppDaemon add-on: Installing libraries/dependencies

I recently got started with AppDaemon (running it as a Hass.io add on) and ran into the issue of not being able to install dependencies that I wanted to import into my apps.

Because I did not want to adjust and rebuild the AppDaemon add on (Dockerfile) each time I wanted to try out/use a dependency, I’ve written a small app that does the installation of dependencies for me.

Posting it here because it might come in helpful for others in the future. I do however think that adjusting and rebuilding the AppDaemon add on is the more correct way of doing this.

Let me know if there’s another way!

import appdaemon.appapi as appapi
import subprocess


PACKAGES = ['guessit',
            'tmdbsimple']


class DepeInstaller(appapi.AppDaemon): 
    def initialize(self):
        self.log("Installing packages")
        
        for package in PACKAGES:
            self.log("Installing {}".format(package))
            self.install(package)

    def install(self, package):
        # https://github.com/pypa/pip/issues/2553
        subprocess.call(['pip3', 'install', package])

Once configured, it should give some output like the following

2017-11-19 11:17:36.590848 INFO Reloading Module: /config/appdaemon/apps/depe_installer.py
2017-11-19 11:17:36.607065 INFO Loading Object depe_installer using class DepeInstaller from module depe_installer
2017-11-19 11:17:36.645915 INFO depe_installer: Installing packages
2017-11-19 11:17:36.660491 INFO depe_installer: Installing guessit
Requirement already satisfied: guessit in /usr/lib/python3.6/site-packages
Requirement already satisfied: rebulk>=0.9.0 in /usr/lib/python3.6/site-packages (from guessit)
Requirement already satisfied: babelfish>=0.5.5 in /usr/lib/python3.6/site-packages (from guessit)
Requirement already satisfied: python-dateutil in /usr/lib/python3.6/site-packages (from guessit)
Requirement already satisfied: six in /usr/lib/python3.6/site-packages (from rebulk>=0.9.0->guessit)
2017-11-19 11:17:41.645966 INFO depe_installer: Installing tmdbsimple
Requirement already satisfied: tmdbsimple in /usr/lib/python3.6/site-packages
Requirement already satisfied: requests in /usr/lib/python3.6/site-packages (from tmdbsimple)
Requirement already satisfied: idna<2.7,>=2.5 in /usr/lib/python3.6/site-packages (from requests->tmdbsimple)
Requirement already satisfied: certifi>=2017.4.17 in /usr/lib/python3.6/site-packages (from requests->tmdbsimple)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/lib/python3.6/site-packages (from requests->tmdbsimple)
Requirement already satisfied: urllib3<1.23,>=1.21.1 in /usr/lib/python3.6/site-packages (from requests->tmdbsimple)
1 Like

shouldnt you add some form off checking if its already installed?
or else it will install everytime you restart AppDaemon.

but i thank you for the idea.
i have some apps i shared that use dependencies. now the people using the app need to do the install themselves and so i can automate it for them.

As you can see in the logging shown above, once a dependency is already installed it won’t do it again (Requirement already satisfied). I agree that checking it would be a cleaner solution.

Didn’t really try it out, but Google/Stackoverflow shows that something like the following could be used:

import importlib

try:
    importlib.import_module(package)
except ImportError:
    self.install(package)
1 Like

I have added this as a dependency in appdaemon, but it seems that the first time when I redeploy appdaemon I still get an error that the dependencies are missing - which resolves itself after a restart. Is there a better way to make sure that this class is run before anything else?