{"id":7286,"library":"hierarchical-conf","title":"Hierarchical Configuration","description":"Hierarchical-conf is a Python library designed for loading settings from YAML files in a hierarchical manner. It processes a list of paths, loading configuration files and overwriting duplicated keys based on file precedence. The library supports Python 3.7+ and is currently at version 1.0.4, with releases appearing to be made as needed.","status":"active","version":"1.0.4","language":"en","source_language":"en","source_url":"https://github.com/quintoandar/hierarchical-conf","tags":["configuration","hierarchical","yaml","environment-variables"],"install":[{"cmd":"pip install hierarchical-conf","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for parsing YAML configuration files, which is the core functionality of the library.","package":"PyYAML","optional":true}],"imports":[{"symbol":"HierarchicalConf","correct":"from hierarchical_conf.hierarchical_conf import HierarchicalConf"}],"quickstart":{"code":"import os\nfrom pathlib import Path\nfrom hierarchical_conf.hierarchical_conf import HierarchicalConf\n\n# Create dummy config files\nproject_root = Path(os.getcwd()) / 'my_configs'\nproject_root.mkdir(exist_ok=True)\n\n(project_root / 'dev_conf.yml').write_text('database: { host: dev_db, port: 5432 }\\nlog_level: DEBUG')\n(project_root / 'production_conf.yml').write_text('database: { host: prod_db, port: 5432 }\\nlog_level: INFO')\n\n# Set environment variable (simulate 'dev' environment)\nos.environ['ENVIRONMENT'] = 'dev'\n\n# Initialize HierarchicalConf\nhconf = HierarchicalConf(conf_files_paths=[str(project_root)])\n\n# Get configuration for 'database'\ndb_config = hconf.get_config('database')\nprint(f\"Database config (dev): {db_config}\")\n\n# Get configuration for 'log_level'\nlog_level = hconf.get_config('log_level')\nprint(f\"Log level (dev): {log_level}\")\n\n# Switch environment to production\nos.environ['ENVIRONMENT'] = 'production'\n\n# Re-initialize to pick up new environment (or load another instance)\nhconf_prod = HierarchicalConf(conf_files_paths=[str(project_root)])\nprod_db_config = hconf_prod.get_config('database')\nprint(f\"Database config (production): {prod_db_config}\")\n\nprod_log_level = hconf_prod.get_config('log_level')\nprint(f\"Log level (production): {prod_log_level}\")\n\n# Clean up dummy files\n# os.remove(project_root / 'dev_conf.yml')\n# os.remove(project_root / 'production_conf.yml')\n# os.rmdir(project_root)\n","lang":"python","description":"This quickstart demonstrates how to set up `hierarchical-conf` to load configurations based on the `ENVIRONMENT` variable. It creates two sample YAML files for 'dev' and 'production' environments, then loads and prints configuration values, showing how the `ENVIRONMENT` variable influences which settings are applied."},"warnings":[{"fix":"Always ensure the `ENVIRONMENT` environment variable is correctly set before initializing `HierarchicalConf`. Configuration files should follow the `[ENVIRONMENT]_conf.yml` naming convention.","message":"The library relies on the `ENVIRONMENT` environment variable to determine which configuration files (e.g., `dev_conf.yml`, `production_conf.yml`) to load. Missetting or omitting this variable will result in unexpected configurations being loaded or no configuration at all.","severity":"gotcha","affected_versions":"All"},{"fix":"Design your configuration hierarchy carefully. If a key needs to be consistent, define it once in the highest-precedence file. If it needs environment-specific overrides, ensure the environment-specific file is loaded later in the precedence.","message":"When multiple configuration files are loaded, keys defined in later loaded files will overwrite values from earlier loaded files if duplicates exist. Understand the loading precedence to avoid unexpected configuration values.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Verify that the `conf_files_paths` list includes the absolute path to the directory containing your YAML files (e.g., `['/path/to/my/configs']`). Confirm that your configuration files are named `dev_conf.yml`, `production_conf.yml`, etc., matching the `ENVIRONMENT` variable.","cause":"The `conf_files_paths` provided to `HierarchicalConf` does not contain the directory where your configuration files are located, or the files are not named according to the `[ENVIRONMENT]_conf.yml` pattern.","error":"KeyError: 'my_config_key' or configuration not loaded as expected."},{"fix":"Inspect all relevant `[ENVIRONMENT]_conf.yml` files for duplicate keys. Trace the loading order based on `conf_files_paths` and the `ENVIRONMENT` variable to understand which value has precedence. Adjust your configuration files or `conf_files_paths` order as needed.","cause":"This often occurs due to key overwriting. If the same key is present in multiple configuration files loaded by `HierarchicalConf`, the value from the last loaded file (highest precedence) will take effect, potentially masking an earlier value you expected.","error":"Configuration value is incorrect despite file existing and environment being set."}]}