try:
    HG_REV = subprocess.check_output(['hg', 'id', '--id']).strip()
except OSError:
    HG_REV = "? (Couldn't find HG)"
except subprocess.CalledProcessError as e:
    pass
Скрипты Mercurial с python