How to run python script health check using pyenv

On a centos7 server I have a service that has a health check that uses a python script. I had a virtualenv using system python 2.7.5 set up and the check seemed to work fine. I recently tried using pyenv to set up python 3.7.5 and I tried setting up a virtualenv using that. I set up the venv for the consul user at /home/consul/venv. The relevant part of my service health check configuration is this:

{
  "service": {
    "name": "mysql_main_master",
    "tags": [
      "mysql_main_master"
    ],
    "address": "10.7.0.19",
    "port": 3306,
    "check": {
      "interval": "1s",
      "timeout": "5s",
      "args": [
        "/home/consul/venv/bin/python",
        "/usr/local/bin/master_healthcheck.py"
      ]
    }
  }
} 

If I su to the consul user and run the command above, it works, but in the consul ui it’s failed with this message:

Could not find platform independent libraries <prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: initfsencoding: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f8f47b17740 (most recent call first):

I tried setting PYTHONHOME=/home/consul/venv/bin/python in the args section, but it didn’t seem to do the trick.

My above solution, which was creating a separate virtualenv for the consul user and using it in the health check actually does work. I had just forgotten to reload consul after applying the new configuration.

1 Like

Welcome to the forum Jon, and congrats on your first post!

Thank you for posting what was missing, I’m sure it’ll help many others!