Skip to content

How to specify a handler package in the various package & directory hierarchies.

Dr. Takeyuki Ueda edited this page Nov 25, 2018 · 1 revision

Specify a handler installed by pip

The handler installed by pip can be specified just simply by name from anywhere as follows.

pi@raspberrypi:~/mh-z19 $ sudo python -m pondslider --sensor_handlers mh_z19
{'co2': 653}

Specify a handler on the python search path

The handler package on the python search path can be specified also just simply by name.
The python search path is listed as follows:

  • The current working directoly
  • The path set on the environment variable PYTHONPATH
  • The path set on the commandline option --imppaths of pondslider, which is set to module search pass internally in the pondslider module by sys.path.append(), will be discribed later.

The sub-package whose root package is on the python search path, can be specified by dot-separate notation like a handlers.value.saver.save2strage.

For example, in case you cloned the handlers example handlers, folder structure should be as follows:

├── handlers
│   ├── __init__.py
│   ├── README.md
│   ├── sensor
│   │   ├── htu21d
│   │   │   └── __init__.py
│   │   ├── __init__.py
│   │   └── mh-z19
│   │       ├── __init__.py
│   │       ├── mh_z19.py
│   │       └── setup.sh
│   └── value
│       ├── __init__.py
│       ├── saver
│       │   ├── __init__.py
│       │   └── save2strage
│       │       ├── __init__.py
│       │       ├── save2strage.ini
│       │       └── save2strage.py
│       └── sender
│           ├── __init__.py
│           ├── send2m2x
│           │   ├── config.toml
│           │   ├── __init__.py
│           │   ├── README.md
│           │   ├── send2m2x.ini
│           │   ├── send2m2x.py
│           │   └── setup.sh
│           └── send2monitor
│               ├── __init__.py
│               ├── README.md
│               ├── send2monitor.ini
│               └── send2monitor.py

The folder handlers is on the current directory. So, you can specify sub-package /handlers/value/saver/save2strage by dot-separate notation as follows:

pi@raspberrypi:~/mh-z19 $ sudo python -m pondslider --sensor_handlers mh_z19 --value_handlers handlers.value.saver.save2strage
{'co2': 504}
81 start saving...
90 214,2018-11-24T12:48:38.263953,504
93 end saving...

Specify a handler which is NOT under the module search path of the Python

In case a module you would like to use is NOT under the module search path, you have to add the path to python search path. There are 2 way to do it as follows:

symbolic link

Make symbolic link of the module to the current working directory.

option --imppaths

Add the path of module to the commandline option --imppaths of pondslider as follows:

pi@raspberrypi:~/mh-z19 $ sudo python -m pondslider --sensor_handlers mh_z19 --value_handlers save2strage --imppaths handlers/value/saver
{'co2': 634}
81 start saving...
90 216,2018-11-24T13:50:51.620844,634
93 end saving...

Internally, the path set to --imppaths is added module search pass by pondslider with sys.path.append() before module loaded.