Favor please testing something for appdaemon

The following code works for me, but it failed for some others. I was wondering if a few of you using appdaemon, could run this from your appdaemon apps directory and see if it works for you? It should just reach over to HA and get your location configuration information. I’m trying to figure out why it works for some and not others.

from homeassistant.util import location 

def loadHAconfig():
    haConfig={}
    locinfo=location.detect_location_info()
    haConfig["IP"]=locinfo.ip
    haConfig["country_code"]=locinfo.country_code
    haConfig["country_name"]=locinfo.country_name
    haConfig["region_code"]=locinfo.region_code
    haConfig["region_name"]=locinfo.region_name
    haConfig["city"]=locinfo.city
    haConfig["zip_code"]=locinfo.zip_code
    haConfig["time_zone"]=locinfo.time_zone
    haConfig["latitude"]=locinfo.latitude
    haConfig["longitude"]=locinfo.longitude
    haConfig["metric"]=locinfo.use_metric
    return haConfig

hainfo=loadHAconfig()
print("hainfo={}".format(hainfo))

I got this error:

2017-01-30 08:24:31.015104 WARNING ------------------------------------------------------------
2017-01-30 10:12:55.007932 WARNING ------------------------------------------------------------
2017-01-30 10:12:55.009351 WARNING Unexpected error during loading of testloc.py:
2017-01-30 10:12:55.009752 WARNING ------------------------------------------------------------
2017-01-30 10:12:55.012545 WARNING Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/appdaemon/appdaemon.py", line 675, in readApp
    conf.modules[module_name] = importlib.import_module(module_name)
  File "/usr/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1471, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "/home/pi/appdaemon/conf/apps/testloc.py", line 1, in <module>
    from homeassistant.util import location
ImportError: No module named 'homeassistant'

2017-01-30 10:12:55.013042 WARNING ------------------------------------------------------------

do you have a $PYTHONPATH environment variable set in the environment where you ran this?

(hass) hass@hass:~/code/appdaemon$ echo $PYTHONPATH
/usr/local/lib/python3.4/dist-packages
(hass) hass@hass:~/code/appdaemon$

No. Also, I am running this a the pi user, not as the hass user.

Try it as the hass user please

where do you run appdaemon from?

From /home/pi/appdaemon

ok run it from pi, but add a pythonpath variable to point to where your python libraries are installed
probably /usr/local/lib/python3.4/dist-packages

export PYTHONPATH=/usr/local/lib/python3.4/dist-packages

Same error.

If you look in that directory, do you have directories similar to the following?

hass) hass@hass:~/code/appdaemon$ ls -l /usr/local/lib/python3.4/dist-packages/home*
/usr/local/lib/python3.4/dist-packages/homeassistant:
total 160
-rw-r–r-- 1 root staff 21886 Dec 21 21:19 bootstrap.py
drwxr-sr-x 28 root staff 4096 Dec 21 21:19 components
-rw-r–r-- 1 root staff 11537 Dec 21 21:19 config.py
-rw-r–r-- 1 root staff 11435 Dec 21 21:19 const.py
-rw-r–r-- 1 root staff 39406 Dec 21 21:19 core.py
-rw-r–r-- 1 root staff 759 Dec 21 21:19 exceptions.py
drwxr-sr-x 3 root staff 4096 Dec 21 21:19 helpers
-rw-r–r-- 1 root staff 36 Dec 21 21:19 init.py
-rw-r–r-- 1 root staff 9044 Dec 21 21:19 loader.py
-rw-r–r-- 1 root staff 12111 Dec 21 21:19 main.py
drwxr-sr-x 2 root staff 4096 Dec 21 21:19 pycache
-rw-r–r-- 1 root staff 17181 Dec 21 21:19 remote.py
drwxr-sr-x 4 root staff 4096 Dec 21 21:19 scripts
drwxr-sr-x 3 root staff 4096 Dec 21 21:19 util

/usr/local/lib/python3.4/dist-packages/homeassistant-0.35.2.dist-info:
total 148
-rw-r–r-- 1 root staff 10 Dec 21 21:19 DESCRIPTION.rst
-rw-r–r-- 1 root staff 54 Dec 21 21:19 entry_points.txt
-rw-r–r-- 1 root staff 4 Dec 21 21:19 INSTALLER
-rw-r–r-- 1 root staff 966 Dec 21 21:19 METADATA
-rw-r–r-- 1 root staff 1241 Dec 21 21:19 metadata.json
-rw-r–r-- 1 root staff 47 Dec 21 21:19 pbr.json
-rw-r–r-- 1 root staff 117944 Dec 21 21:19 RECORD
-rw-r–r-- 1 root staff 14 Dec 21 21:19 top_level.txt
-rw-r–r-- 1 root staff 110 Dec 21 21:19 WHEEL
(hass) hass@hass:~/code/appdaemon$

There’s no homeassistant directory at that path. I am running a pre-December 2016 AIO install if that helps.

Ok, that explains a few things. Now to figure out where the python libraries are on your system… Login as your homeassistant user and see if it’s in there then?

Not sure I understand what you mean here.

the user you run home assistant as. I’m not familiar with the AIO install so I’m kind of flying blind here. But the library where the python libraries are for python is probably where we need to be.

Yeah I got the part about which user, I wasn’t understanding what to do once I was logged in. The ls command is going to show the same thing no matter which user I’m logged in as, right?

I’m not sure. If it does a virtual environment kind of thing it might not.

Can’t seem to find it. I thought this might be the path:

/srv/hass/hass_venv/lib/python3.4/site-packages

But when I set the PYTHONPATH to this it still failed with the same error, even though this seems to match the contents of the example you gave:

pi@raspi1:/$ ls -l /srv/hass/hass_venv/lib/python3.4/site-packages/home*
/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant:
total 164
-rw-r--r--  1 hass hass 21985 Jan 29 08:36 bootstrap.py
drwxr-xr-x 29 hass hass  4096 Jan 29 08:36 components
-rw-r--r--  1 hass hass 15432 Jan 29 08:36 config.py
-rw-r--r--  1 hass hass 11622 Jan 29 08:36 const.py
-rw-r--r--  1 hass hass 39484 Jan 29 08:36 core.py
-rw-r--r--  1 hass hass   759 Jan 29 08:36 exceptions.py
drwxr-xr-x  3 hass hass  4096 Jan 29 08:36 helpers
-rw-r--r--  1 hass hass    36 Jan 29 08:36 __init__.py
-rw-r--r--  1 hass hass  9044 Jan 29 08:36 loader.py
-rw-r--r--  1 hass hass 12156 Jan 29 08:36 __main__.py
drwxr-xr-x  2 hass hass  4096 Jan 29 08:36 __pycache__
-rw-r--r--  1 hass hass 17269 Jan 29 08:36 remote.py
drwxr-xr-x  4 hass hass  4096 Jan 29 08:36 scripts
drwxr-xr-x  3 hass hass  4096 Jan 29 08:36 util

/srv/hass/hass_venv/lib/python3.4/site-packages/homeassistant-0.37.0.dist-info:
total 160
-rw-r--r-- 1 hass hass     10 Jan 29 08:36 DESCRIPTION.rst
-rw-r--r-- 1 hass hass     54 Jan 29 08:36 entry_points.txt
-rw-r--r-- 1 hass hass      4 Jan 29 08:36 INSTALLER
-rw-r--r-- 1 hass hass    984 Jan 29 08:36 METADATA
-rw-r--r-- 1 hass hass   1259 Jan 29 08:36 metadata.json
-rw-r--r-- 1 hass hass     47 Jan 29 08:36 pbr.json
-rw-r--r-- 1 hass hass 128134 Jan 29 08:36 RECORD
-rw-r--r-- 1 hass hass     14 Jan 29 08:36 top_level.txt
-rw-r--r-- 1 hass hass    110 Jan 29 08:36 WHEEL
pi@raspi1:/$

ok, very strange. I’m gonna have to get on the developers board and figure out what they are doing…
Thanks

NP. Hope it helped.

I put the file in my apps directory of my test box and got this in the log file with no errors

2017-01-30 17:17:20.013334 INFO Reloading Module: /home/homeassistant/.homeassis
tant/apps/haconfig.py

Is that a successful test?

edit:
I have no PYTHONPATH, but appdaemon and hass are both running in the same virtual env.