Check config error: /usr/local/bin/python: No module named homeassistant

So this seems a little weird. HA is running inside from the official docker image (latest) and has been fine for awhile. I made a change to support the insteon_local module and hit check config, and got that error.
_However if I run the check config script manually (docker exec -ti home-assistant1 python -m homeassistant --config /config --script check_config) the script runs fine (and finds errors if there are any). I can restart HA fine from docker, though not from the GUI. The only error is in the home-assistant.log file:

2018-02-07 02:32:24 ERROR (MainThread) [homeassistant.components] /usr/local/bin/python: No module named homeassistant

Logging into the docker image with bash I run the python console and import the home assistant module with no errors.

root@rs-01:/srv/ha# docker exec -i -t home-assistant1 /bin/bash                                                                       
root@rs-01:/usr/src/app# python
Python 3.6.4 (default, Dec 21 2017, 01:35:12) 
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import homeassistant
>>> 

What the heck is going on? It’s running ok but that error doesn’t sit well with me.

I ran into this while building an GitLab CI runner. Christ almighty, I wasted 2 hours debugging this.
But (!) the solution in the end turns out to be ridiculously simple:

You have to be in the right directory in order for python to load the module.

Try
docker exec -w /usr/src/app -ti home-assistant1 python -m homeassistant --config /config --script check_config

From what I gathered from all of this docker fuckery is that your image dockerfile lacks one instruction:
WORKDIR /usr/src/app

Or not. What the hell do I know ¯\(ツ)

Check the official one for reference:
https://hub.docker.com/r/homeassistant/home-assistant/~/dockerfile/

Now. If anyone ever stumbles across this thread trying to build a .gitlab-ci.yml here’s the one I came up with:
image:
homeassistant/home-assistant

stages: 
  - test

hass_check_config:
  stage: test
  script: 
  - cd /usr/src/app
  - /usr/local/bin/python -m homeassistant --config /builds/hass/config --script check_config

Just swap the hass/config part in --config /builds/hass/config for your repo path (group/repo).
That’s where the CI runner will spew the ‘build’ a.k.a your config.

2 Likes

Damn, tried specifying the working dir and it still spits out the error, but only on config checks and attempts to do any restarts / reloads of the config. Running it with a standard docker run command starts it up with no problems, and a docker restart command obviously restarts it with no problems. It’s really strange.

Thank you, the cd was the missing part for me.

test:
  stage: test
  script:
    - cd /usr/src/app
    - cp $CI_PROJECT_DIR/secrets_dist.yaml $CI_PROJECT_DIR/secrets.yaml
    - /usr/local/bin/python -m homeassistant --config $CI_PROJECT_DIR/ --script check_config
  artifacts:
    paths:
       - ./*

Just an update in case someone stumbles on this - somewhere along the way this was fixed. No idea what broke or what was fixed unfortunately.

You don’t want to use cd as it’ll break things if your config uses anything referenced by path, such as MQTT certs. Instead, set export PYTHONPATH=/usr/src/app in your GitLab-CI file:

test:
  stage: test
  image: $CONTAINER_IMAGE:latest
  script:
    - export PYTHONPATH=/usr/src/app
    - cp CI_secrets.yaml secrets.yaml     #Fake secrets.yaml for privacy
    - touch customize.yaml     #Excluded via .gitignore as it contains private info
    - /usr/local/bin/python -m homeassistant --config $CI_PROJECT_DIR/ --script check_config

This way you can test both your Docker container build, and your specific config.