(Sep-21-2020, 05:25 AM)buran Wrote: Also, show as your imports with respect tow1thermsensor
orw1thermsensor.errors
Looking at the project __init__.py they import their custom errors at to level, so
import w1thermsensor try: my_sensor = w1thermsensor.W1ThermSensor() except w1thermsensor.NoSensorFoundError print('No sensor found')
Here is my version of that:
from w1thermsensor import W1ThermSensor try: sensor = W1ThermSensor(W1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945') except w1thermsensor.errors.NoSensorFoundError: print('Thermometer not Found')here is the output:
Error:Traceback (most recent call last):
File "curl.py", line 5, in <module>
sensor = w1ThermSensor(w1ThermSensor.THERM_SENSOR_DS18B20, '0300a27989945')
NameError: name 'w1ThermSensor' is not defined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "curl.py", line 6, in <module>
except w1thermsensor.errors.NoSensorFoundError:
NameError: name 'w1thermsensor' is not defined
Here is what is in the modules __init__.py filefrom .__version__ import __version__ # noqa from .core import W1ThermSensor # noqa from .errors import NoSensorFoundError # noqa from .errors import SensorNotReadyError # noqa from .errors import UnsupportedUnitError # noqa from .errors import ResetValueError # noqahere is a file called errors.py in the module dir
# -*- coding: utf-8 -*- """ This module provides exceptions for the w1thermsensor. """ import textwrap class W1ThermSensorError(Exception): """Exception base-class for W1ThermSensor errors""" pass class KernelModuleLoadError(W1ThermSensorError): """Exception when the w1 therm kernel modules could not be loaded properly""" def __init__(self): super(KernelModuleLoadError, self).__init__( "Cannot load w1 therm kernel modules" ) class NoSensorFoundError(W1ThermSensorError): """Exception when no sensor is found""" def __init__(self, message): super(NoSensorFoundError, self).__init__( textwrap.dedent( """ {} Please check cabling and check your /boot/config.txt for dtoverlay=w1-gpio """.format( message ) ).rstrip() ) class ResetValueError(W1ThermSensorError): """Exception when the reset value is yield from the hardware""" def __init__(self, sensor): super(ResetValueError, self).__init__( "Sensor {} yields the reset value of 85 degree millicelsius. " "Please check the hardware.".format(sensor.id) ) self.sensor = sensorI can't really find where the errors are called