Python Debugger in Hassio?

Are there any python debuggers that I’d be able to install and use within Hassio? I basically just want to be able to step through python code in custom_components and be able to set breakpoints and view values of variables.

Wasn’t sure if this is even possible or whether I would have to move away from hassio if I want to use a debugger of some sort? Also is there a hassio debugger addon?

1 Like

I thought all python developers debugged using the python command line too.=l.

How does that work exactly? I’m a noob. Googling says to add
import pdb; pdb.set_trace() where I want a breakpoint and pdb shell will appear upon execution.

Does that work under hassio? I’m also wondering if I execute the custom component py script from an ssh session, will it still get its configuration from configuration.yaml or does it only read its config if it is called by Home Assistant during the update interval?

Sorry, I have done very little programming in python. Apparently, pdb is the python debugger. I was unaware of that tool.

It appears as though I can run python from the web terminal addon. Then using, “python -m pdb sensor.py” I can step through the code with pdb.

Is there any way for importing of the config and entity to work?:

import homeassistant.helpers.config_validation as cv
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.helpers.entity import Entity

If not I guess, I should just comment those lines out and hard code the config values into the script just while debugging?

Sorry for the noob questions. Just trying to get my feet wet.

Personally, I debug custom components by writing to the logs. But I’ll follow this in case you manage get it working :grin:.

Instead of doing that from the terminal add-on, have you tried from the Home Assistant container itself? You may need to figure out how to restart HA within the container since a brief search suggests pdb can’t attach to an already running process.

1 Like

What is the command to do that? Which log? Thanks!

Just to the regular HA logs:

import logging

_LOGGER = logging.getLogger(__name__)

_LOGGER.debug('message')
1 Like

i’ve been looking for an answer to this question as well. The documentation is not very detailed. LOGGER is an option, but not enough. I am looking to stop the flow and actually look at dictionaries and data structures to understand their integrity and formatting. Can;t do all that with simple debug prints. There are complex components in home assistant, there MUST be a way to do realtime debug somehow

If you do a manual installation of Home Assistant in a venv you can start it with the hass command. Perhaps there is a way to use pdb.

I did the manual installation in my laptop but it seems that pip installs hass as a binary :

~/which hass
 /home/cagabi/.local/bin/hass 

so I don’t know how to start it with pdb, I try but i get an error as if hass is not a python module

~/python3 -m pdb hass
Error: hass does not exist

Are you sure it is a binary or script?
file /home/cagabi/.local/bin/hass should tell you if it is a binary or not.

The second command just means there is no python module named hass.

Very good point @anon34565116. I assumed it was a binary because it’s in the bin directory and python3 hass returns an error. So running your command;

~$ file /home/cagabi/.local/bin/hass 
/home/cagabi/.local/bin/hass: Python script, ASCII text executable

But then doing pip3 freeze I see that homeassistant is installed and I can start it with python3 -m homeassistant in the same way that just running hass

But when I do

~$ python3 -m pdb homeassistant
Error: homeassistant does not exist

Are you in the venv where homeassistant is installed?

Sorry for never getting back to this.
Finally debug it is working but I am not starting pdb from the command line, I only have to import pdb in the script and call pdb.set_trace(). Simply running hass in the command line stops at the breakpoint

Hi cagabi,
please can you say what do you mean with " running hass in the command line" ?