Skip to main content
zig-hocon

Read HOCON config files in Python, to the spec. Fast, because the parser is written in Zig.

HOCON is JSON made for people: comments, no quotes needed, units like 30s and 512MiB, files that include other files, later settings that merge over earlier ones, and values taken from other keys or from environment variables. One base file, one small file per environment on top.

# app.conf
db {
  host = localhost
  pool = 4
}
timeout = 30s
cache = 512MiB
retention = 3 months
debug = false
ratio = 0.75
# prod.conf
include "app.conf"
db.host = db.example.org
db.pool = ${?DB_POOL}   # only when DB_POOL is set
from zig_hocon import ConfigFactory

conf = ConfigFactory.parse_file("prod.conf")
conf.get_string("db.host")   # "db.example.org"
conf.get_int("db.pool")      # 4, or $DB_POOL when it is set

The rest of this page reads these two files. Variables go last. A later setting hides an earlier one, a ${?…} too: workers = ${?WORKERS} in app.conf under workers = 8 in prod.conf is 8 whatever WORKERS says. Put what the environment overrides where nothing comes after it, as prod.conf here does with ${?DB_POOL}, or in a file every environment includes last.

Why zig-hocon

  • Correct. Passes all 588 tests in the conformance suite: each checks one sentence of the spec, and together they cover 276 of its 290 rules.
  • Fast. Orders of magnitude faster than what Python usually has.*
  • Clear errors. File, line, column, and a caret under the mistake.
  • No dependencies. A thin ctypes layer over one bundled library.

* A 1 MB config with substitutions in 13 ms, a 10 MB one in 0.14 s, on an Apple M4 Pro: BENCHMARK.md.

Install

pip install zig-hocon

It puts two commands on the path: hocon, the command line tool of zig-hocon built for your platform, and zig-hocon-migrate, the check of what changes from pyhocon (Coming from pyhocon).

One wheel per platform, for every Python from 3.10 on, free-threaded builds and PyPy included: macOS (arm64, x86_64), Linux (x86_64, aarch64; glibc 2.17+ and musl) and Windows (x86_64). The import name is zig_hocon. Anywhere else, build it from source (below).

Beta: the API can still change before 1.0. BSD-3-Clause.

Use

from zig_hocon import ConfigFactory, KEEP

conf = ConfigFactory.parse_file("prod.conf")                       # DB_POOL from os.environ
conf = ConfigFactory.parse_file("prod.conf", env={"DB_POOL": "8"}) # from a dict
conf = ConfigFactory.parse_file("prod.conf", env=None)             # no variables: pool stays 4
conf = ConfigFactory.parse_file("prod.conf", env=KEEP)             # ${?DB_POOL} kept, for to_hocon(); a getter there is Unresolved
conf = ConfigFactory.parse_file("prod.conf", resolve=False)        # every ${…} as written
conf = ConfigFactory.parse_file("prod.conf", http_timeout=5)       # an http(s) include waits 5 s, not 30
conf = ConfigFactory.parse_string('include "prod.conf"', basedir=".")  # text, includes from basedir

conf.get_string("db.host")      # "db.example.org"
conf.get_int("db.pool")         # 4
conf.get_float("ratio")         # 0.75
conf.get_bool("debug")          # False: true/yes/on, false/no/off
conf.get_duration("timeout")    # timedelta(seconds=30)
conf.get_bytes("cache")         # 536870912: "512MB" counts by 1000, "512MiB" by 1024
conf.get_period("retention")    # Period(years=0, months=3, days=0): a timedelta has no months
conf.get_config("db")           # the object at db, read the same way
conf.get_int("db.size", default=10)  # 10: nothing at db.size

conf.as_dict()                  # plain dicts and lists
conf.to_json()                  # bytes, not str: for msgspec.json.decode
conf.to_hocon()                 # HOCON text

A path is written as in the file: db.host, a."b.c". A getter's default is its second argument, as in pyhocon; parse_file and parse_string take everything after the path by keyword. The package is typed (py.typed): get_int("a") is an int, get_int("a", None) an int | None. One document can be read from many threads at once.

Errors are ConfigExceptions, with name, file, line, column and source_line:

exception when
ConfigParseException the text is not valid HOCON, or an include fails
ConfigSubstitutionException a ${…} with no value, or a cycle
ConfigMissingException (also a KeyError) nothing at the path, or null
ConfigWrongTypeException (also a ValueError) the value is not what the getter wants

One file to deploy. env=KEEP resolves includes, overrides and references between keys, and leaves the environment variables in; to_hocon() writes the result, to be read on the server with its own environment:

from pathlib import Path

Path("deploy.conf").write_text(ConfigFactory.parse_file("prod.conf", env=KEEP).to_hocon())

Durations, periods and sizes have helpers with the same rules as the getters: zig_hocon.parse_duration("30s") gives a timedelta, zig_hocon.parse_period("3 months") a Period as get_period does, zig_hocon.parse_bytes("512MiB") an int; zig_hocon.parse_int and zig_hocon.parse_bool read a number and a boolean as get_int and get_bool do (Strict, as the Zig side reads it). A duration is nanoseconds in an int64, as in C: a timedelta to the microsecond, rounded half to even (1500ns is 2 µs, where Zig keeps 1500 ns), of up to 292 years; a longer one is a ConfigWrongTypeException named Overflow.

From the shell, hocon prints the file resolved, and more; python -m zig_hocon [--json] [--env=process|none|keep] prod.conf does the first through the package.

The hocon command

pip install zig-hocon puts hocon on the path: the same tool as a release of zig-hocon has, built for your platform. It prints a file resolved, as HOCON or with --json as JSON:

$ hocon --json prod.conf
{"db":{"host":"db.example.org","pool":4},"timeout":"30s","cache":"512MiB","retention":"3 months","debug":false,"ratio":0.75}

A ${…} the file does not answer reads the environment; --env=none reads none. --env-file=prod.env puts the KEY=value lines of a file over it, as docker compose and python-dotenv read them; a file written for a shell's source, with export and quotes, works as it is. A later file goes over an earlier one, and with --env=none they are all there is: hocon check --env=none --env-file=prod.env prod.conf checks a deployment against its own variables and nothing from your shell.

# prod.env
DB_HOST=db.internal
DB_POOL=16
# a line written for `source` reads the same
export LOG_LEVEL="warn"

--env=keep writes the one file to deploy: includes and overrides worked out, the variables left for the server:

$ hocon --env=keep prod.conf > deploy.conf
$ cat deploy.conf
db {
  host = db.example.org
  pool = 4
  pool = ${?DB_POOL}
}
timeout = 30s
cache = 512MiB
retention = 3 months
debug = false
ratio = 0.75

get prints one value, for a shell script:

$ hocon get db.host prod.conf
db.example.org
$ hocon get db --json prod.conf
{"host":"db.example.org","pool":4}

diff compares two files as they resolve, path by path (~ path: old → new, + and -), and exits 0 when they say the same, as the deploy file does, 1 when they differ and 2 when one cannot be read:

$ hocon diff prod.conf deploy.conf; echo $?
0

check reads each file and says which fail and where, for CI:

$ hocon check prod.conf staging.conf
prod.conf: ok
staging.conf:2:11: error: Unresolved
db.host = ${DB_HOST}
          ^
1 of 2 failed

Each exits 1 when something is wrong (diff 2, as 1 is a difference), with the place on stderr; hocon alone lists the commands and their flags. (The hocon of a release also has check --schema, which reads a file into a Zig struct; it needs zig, and the wheel's leaves it out.)

Coming from pyhocon

Moving over is a rewrite of the code that works with the tree, not a change of import. What carries over: ConfigFactory.parse_file and parse_string, the typed getters (get_string, get_int, get_float, get_bool, get_config) and the exception names. What does not:

  • The tree is not a dict, and get, get_list, with_fallback and put are not there: a typed getter, conf.as_dict() or an include does the job.
  • A list has no getters. Read it through your model (list[str]), which checks the type as get_list did; conf.as_dict() gives whatever is there, a string from the environment too.
  • Durations stay text. 30s is "30s" in as_dict() until a getter or a model reads it.
  • += appends one element. x += [a], the form pyhocon takes, adds the list [a] as one element; x = ${x} [a] extends it.
  • Values from the environment are text, read as numbers and booleans only as written: 08 or True stop get_int and get_bool. Check your secret store first.
  • Strict syntax, and no include package(...).

zig-hocon-migrate, which pip installs with zig-hocon, reads your configs with pyhocon and with zig-hocon and shows what differs:

zig-hocon-migrate conf/prod.conf --env-file env/prod.env

MIGRATING.md has every difference, what to write instead, and how to run the check for each deployment in CI.

Into a model

The package hands over a dict (as_dict()) or JSON bytes (to_json()); a model library does the checking. The same prod.conf into the three usual ones.

pydantic reads the dict. A duration is text in the file, "30s", so a BeforeValidator turns it into a timedelta first:

from datetime import timedelta
from typing import Annotated

import zig_hocon
from pydantic import BaseModel, BeforeValidator

Duration = Annotated[timedelta, BeforeValidator(zig_hocon.parse_duration)]

class Db(BaseModel):
    host: str
    pool: int

class Settings(BaseModel):
    db: Db
    timeout: Duration
    debug: bool

conf = zig_hocon.ConfigFactory.parse_file("prod.conf")
settings = Settings.model_validate(conf.as_dict())

msgspec decodes the JSON bytes straight into a Struct. It reads a timedelta itself only from ISO 8601 ("PT30S"), so the field gets a type of its own that msgspec hands to dec_hook. An environment variable is always a string, so strict=False lets DB_POOL=16 into the int:

from datetime import timedelta

import msgspec
import zig_hocon

class Duration(timedelta):
    pass

def dec_hook(type, value):
    if type is Duration:
        d = zig_hocon.parse_duration(value)
        return Duration(d.days, d.seconds, d.microseconds)
    raise NotImplementedError(type)

class Db(msgspec.Struct):
    host: str
    pool: int

class Settings(msgspec.Struct):
    db: Db
    timeout: Duration
    debug: bool

conf = zig_hocon.ConfigFactory.parse_file("prod.conf")
settings = msgspec.json.decode(conf.to_json(), type=Settings, dec_hook=dec_hook, strict=False)

@dataclass converts no types and does not build Db from a dict, so msgspec builds it, with the Duration, dec_hook and conf from above:

from dataclasses import dataclass

@dataclass
class Db:
    host: str
    pool: int

@dataclass
class Settings:
    db: Db
    timeout: Duration
    debug: bool

settings = msgspec.json.decode(conf.to_json(), type=Settings, dec_hook=dec_hook, strict=False)

All three give settings.db.host == "db.example.org" and settings.timeout == timedelta(seconds=30), and fail with the model's own error, path included, on a value of the wrong type. A size works the same way through zig_hocon.parse_bytes. The examples have these as tests, with layered environments, errors and one-file deploys.

Strict, as the Zig side reads it. A model's own rules for a number or a boolean are not the library's, and a value from the environment is always text, ${DB_POOL} with 08 in it is "08":

text get_int / get_bool, a Zig struct pydantic msgspec, strict=False
"08", "+9000", "9_000", " 48" refused 8, 9000, 9000, 48 refused
"True", "1" refused True True
"yes", "on" True True refused

So the same file, with the same environment, can start the Python side with workers = 8 where the Zig side of the application refuses to start, or the other way round. zig_hocon.parse_int and zig_hocon.parse_bool read as get_int, get_bool and a Zig struct do; as validators they make a model take exactly what the Zig side takes:

from typing import Annotated

import zig_hocon
from pydantic import BaseModel, BeforeValidator, Field

Int = Annotated[int, BeforeValidator(zig_hocon.parse_int)]
Bool = Annotated[bool, BeforeValidator(zig_hocon.parse_bool)]

class Db(BaseModel):
    host: str
    pool: Annotated[Int, Field(ge=0, le=65535)]  # a u16 on the Zig side

class Settings(BaseModel):
    db: Db
    debug: Bool

The range stays the model's (Field). With msgspec, the same calls go in dec_hook, for types of their own as Duration above. Text is the other way round: unquoted password = 12345 is the text "12345" to a Zig []const u8 field and to get_string, but a number to as_dict(), where 1.50 is also 1.5, so a model's str field cannot see what was written. Quote a value meant as text that looks like a number, password = "12345", and both sides read the same text.

Building from source

zig build python -Doptimize=ReleaseSafe   # libhocon into python/zig_hocon/
pip install python/
zig build test-python                     # the package's tests, with uv
zig build wheels                          # a wheel for every platform, into zig-out/wheels/

Needs Zig 0.16. Source, issues and the Zig and C libraries: github.com/lejmr/zig-hocon. BSD-3-Clause.

Metadata

Release files for zig-hocon 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distributions (wheels)

Table of built distributions (wheels) for zig-hocon 0.6.0
File Actions
zig_hocon-0.6.0-py3-none-win_amd64.whl Python 3 none Windows x86-64 Details for zig_hocon-0.6.0-py3-none-win_amd64.whl
zig_hocon-0.6.0-py3-none-musllinux_1_2_x86_64.whl Python 3 none Linux musl 1.2+ x86-64 Details for zig_hocon-0.6.0-py3-none-musllinux_1_2_x86_64.whl
zig_hocon-0.6.0-py3-none-musllinux_1_2_aarch64.whl Python 3 none Linux musl 1.2+ ARM64 Details for zig_hocon-0.6.0-py3-none-musllinux_1_2_aarch64.whl
zig_hocon-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl Python 3 none Linux glibc 2.17+ x86-64 Details for zig_hocon-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
zig_hocon-0.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl Python 3 none Linux glibc 2.17+ ARM64 Details for zig_hocon-0.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
zig_hocon-0.6.0-py3-none-macosx_13_0_x86_64.whl Python 3 none macOS 13.0+ x86-64 Details for zig_hocon-0.6.0-py3-none-macosx_13_0_x86_64.whl
zig_hocon-0.6.0-py3-none-macosx_13_0_arm64.whl Python 3 none macOS 13.0+ ARM64 Details for zig_hocon-0.6.0-py3-none-macosx_13_0_arm64.whl

Total release size: 7.5 MB

Release files / zig_hocon-0.6.0-py3-none-win_amd64.whl

Download URL zig_hocon-0.6.0-py3-none-win_amd64.whl
Size 1.2 MB
Tags Python 3 Windows x86-64
SHA-256 checksum
How to use checksums
1df3c43b59bc5c6c3902e3cb8827882dc7a9fe4d8043b7b76837ea0d130ed4f4
BLAKE2b-256 checksum
How to use checksums
38b0479844ea46ea067065a6795d0383d21e3c60c0edee8acae658e878c9caa1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zig_hocon-0.6.0-py3-none-musllinux_1_2_x86_64.whl

Download URL zig_hocon-0.6.0-py3-none-musllinux_1_2_x86_64.whl
Size 1.1 MB
Tags Linux musl 1.2+ x86-64 Python 3
SHA-256 checksum
How to use checksums
03b75b88c0d29882b09ce05d9e2adbe8d943db9d0ead46e838073501a3f2ee35
BLAKE2b-256 checksum
How to use checksums
6541cd4d2c08eeac868d5f45a5f527115c57c8ab28cf0a2e352976bf6490d762
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zig_hocon-0.6.0-py3-none-musllinux_1_2_aarch64.whl

Download URL zig_hocon-0.6.0-py3-none-musllinux_1_2_aarch64.whl
Size 1.1 MB
Tags Linux musl 1.2+ ARM64 Python 3
SHA-256 checksum
How to use checksums
9e96176cbaff79ef3b4dcc964abbd4f876e32a5a2724343ba7998a9949b292cb
BLAKE2b-256 checksum
How to use checksums
505a401677728d0ae4a25cab2462b31938de55dc2fb353af5a499313a194f15b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zig_hocon-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL zig_hocon-0.6.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 1.1 MB
Tags Linux glibc 2.17+ x86-64 Python 3
SHA-256 checksum
How to use checksums
54932c4a94f6492aba95d369d5b0116a3f35386b248192ce1223020d7db865c9
BLAKE2b-256 checksum
How to use checksums
290d89e5a15017ccd5c34bdce8ae2c249ac64b254c39767946ae8f83da4a2239
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zig_hocon-0.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL zig_hocon-0.6.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 1.1 MB
Tags Linux glibc 2.17+ ARM64 Python 3
SHA-256 checksum
How to use checksums
fb361b34c3aaab2890ae2bb9b61720230e718542e53d1341df55ee1195f87898
BLAKE2b-256 checksum
How to use checksums
bab1eaf775b58647c8187ebeb8218400ebd1a13b7ee0968f003c2674bd5f6c17
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zig_hocon-0.6.0-py3-none-macosx_13_0_x86_64.whl

Download URL zig_hocon-0.6.0-py3-none-macosx_13_0_x86_64.whl
Size 1.1 MB
Tags Python 3 macOS 13.0+ x86-64
SHA-256 checksum
How to use checksums
6a664ef1414736c019d9d37a9a744016582cff039c5e8ad7fed35ffadf4edc08
BLAKE2b-256 checksum
How to use checksums
f32e512ff1519079afa81dd10d767eb5568cb2260987293290f607fd304626e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Release files / zig_hocon-0.6.0-py3-none-macosx_13_0_arm64.whl

Download URL zig_hocon-0.6.0-py3-none-macosx_13_0_arm64.whl
Size 972.4 kB
Tags Python 3 macOS 13.0+ ARM64
SHA-256 checksum
How to use checksums
63f0159a4f3cf5b1c1ddb5d00ab19638e807a83d8fea624d2f1bd88e02916cdc
BLAKE2b-256 checksum
How to use checksums
2338bce5bb98e0f1c817b4dbc68d5490b364d6aec303971777b6f1b412c86d4e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

Security for zig-hocon 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 7 files in this release have been uploaded with provenance metadata.

Select from 7 files

Attestation for zig_hocon-0.6.0-py3-none-win_amd64.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 29, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page