Reusable dynamic config provider with YAML fallback and Nacos backends
Project description
dynamic-config-nacos
dynamic-config-nacos is a small reusable Python package for dynamic
configuration loading.
Its goal is to give application projects one consistent way to read configuration, whether the source is a local YAML file or a Nacos server.
Author: FrankTang (franktz2003@gmail.com)
License: MIT
What This Library Does
- Uses local YAML as a fallback configuration source
- Fetches remote configuration from Nacos
- Supports automatic backend selection across
http,sdk_v2, andsdk_v3 - Refreshes in-memory configuration on updates on a best-effort basis
- Provides a lightweight
Confobject for dot-path and index-based access
Install
pip install dynamic-config-nacos
The Python import name remains dynamic_config:
from dynamic_config import DynamicConfigProvider
For local workspace development with uv, downstream projects can also use a
path dependency to this package.
Quick Start
from dynamic_config import DynamicConfigProvider
provider = DynamicConfigProvider(local_yaml_path="configs/local.yaml")
provider.load_from_env()
app_name = provider.get("app.name")
Usage
Local YAML Only
from dynamic_config import DynamicConfigProvider
provider = DynamicConfigProvider(local_yaml_path="configs/local.yaml")
provider.load_initial(None)
debug = provider.get("app.debug", False)
Enable Nacos with Environment Variables
$env:NACOS_SERVER_ADDR = "127.0.0.1:8848"
$env:NACOS_DATA_ID = "app.yaml"
$env:NACOS_GROUP = "DEFAULT_GROUP"
from dynamic_config import DynamicConfigProvider
provider = DynamicConfigProvider(local_yaml_path="configs/local.yaml")
provider.load_from_env()
app_name = provider.get("app.name")
Override the Local YAML Path
DynamicConfigProvider currently requires a local_yaml_path value during
construction, but load_from_env() will override it if LOCAL_CONFIG_PATH is
set.
$env:LOCAL_CONFIG_PATH = "configs/dev.local.yaml"
After load_from_env(), the library will use the path from
LOCAL_CONFIG_PATH.
Read Values Through Conf
conf = provider.conf
value1 = conf["a.b[0].c"]
value2 = conf.a.b[0].c
value3 = conf.get("a.x", "fallback")
Logging
This package uses the standard library logging module and does not currently
accept a custom logger object from callers.
Internally it uses module-level loggers like this:
logger = logging.getLogger(__name__)
That means the host application controls log output, handlers, formatting, and log levels:
import logging
logging.basicConfig(level=logging.INFO)
Typical log events include:
warningwhen the local YAML file is missingwarningwhen Nacos returns a non-mapping YAML rootwarningfor invalid backend or polling interval valuesinfowhen a watcher has started, including the backend and polling interval for HTTP modeinfowhen a Nacos update has been appliedinfowhen a backend is auto-selectedexceptionwhen Nacos fetch, watcher startup, login, or version detection fails
Separate nacos-python-sdk Logs
If you use sdk_v2 or sdk_v3, you can route SDK logs to a dedicated path
without changing your application's root logger setup:
from dynamic_config import DynamicConfigProvider, NacosBackendType, NacosSettings
provider = DynamicConfigProvider(local_yaml_path="configs/local.yaml")
provider.load_initial(
NacosSettings(
server_addr="127.0.0.1:8848",
namespace=None,
data_id="app.yaml",
group="DEFAULT_GROUP",
backend=NacosBackendType.SDK_V3,
sdk_log_level="ERROR",
sdk_log_path="logs/nacos.log",
)
)
You can also configure the same behavior through environment variables:
$env:NACOS_SDK_LOG_LEVEL = "ERROR"
$env:NACOS_SDK_LOG_PATH = "logs/nacos.log"
sdk_log_path accepts either a directory or an explicit log file path. When
you pass a file path such as logs/nacos.log, the SDK logs are written to that
exact file.
SDK Compatibility Note
If you install the current nacos-sdk-python 3.x line, the supported SDK path
in this package is sdk_v3.
In auto mode, the package now prefers sdk_v3 when that is the only
installed SDK-backed import path available. sdk_v2 is retained for older
environments that still provide the legacy nacos package import.
Does the Local YAML File Need to Exist?
local_yaml_pathis currently a required constructor argument- The file itself does not need to exist
- If the file is missing, the library logs a warning and falls back to an empty
config
{} - If Nacos successfully returns a config, the local YAML file is not used for that load
In practice, the path behaves more like a local fallback location than a strict required input file.
Internal Design Overview
The library's loading flow looks like this:
- Create
DynamicConfigProviderwith a local YAML path. - Call
load_from_env()to read Nacos-related environment variables. - If
NACOS_SERVER_ADDRis present, build aNacosSettingsobject. - Create a Nacos backend using explicit configuration or auto-detection.
- Try to fetch YAML content from Nacos first.
- If Nacos fails, returns empty content, or returns a non-mapping YAML root, fall back to the local YAML file.
- Store the final raw dictionary and wrap it in a
Confobject. - If the backend supports watching, start a watcher and refresh the in-memory config on updates.
Auto Backend Selection
When backend=AUTO, the package first tries to detect the Nacos server major
version over HTTP and then picks a preferred order:
- Nacos 2.x:
sdk_v2->sdk_v3->http - Nacos 3.x:
sdk_v3->sdk_v2->http - Detection failed:
sdk_v3->sdk_v2->http
After building that preferred order, the package filters out SDK paths that are
not actually importable in the current Python environment. For example, if only
v2.nacos is installed, auto skips sdk_v2 and goes straight to sdk_v3.
HTTP Mode
- Fetches config through the Nacos HTTP API
- Optionally logs in first to obtain an
accessToken - Starts a background polling thread
- Logs watcher startup with the active backend and polling interval
- Uses content MD5 to detect changes
- Logs when an updated config has been applied
SDK Mode
- Tries multiple SDK import paths
- Tries both
get_configandgetConfig - Tries
add_config_watchers,add_config_watcher, andadd_listener
Public API
DynamicConfigProviderConfNullConfNULLNacosSettingsNacosBackendType
Additional Docs
- English usage guide: how-to-use.md
- Chinese usage guide: how-to-use.zh-CN.md
- Chinese overview: README.zh-CN.md
- Changelog: CHANGELOG.md
Project Links
- Homepage: https://github.com/franktz/dynamic-config-nacos
- Repository: https://github.com/franktz/dynamic-config-nacos
- Issues: https://github.com/franktz/dynamic-config-nacos/issues
Author
- FrankTang
- Email: franktz2003@gmail.com
- License: MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dynamic_config_nacos-0.1.3.tar.gz.
File metadata
- Download URL: dynamic_config_nacos-0.1.3.tar.gz
- Upload date:
- Size: 27.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
631c772bafd5395bfa6b6ae95a26402205f2dbc3f1094ded3cdbc8a38fe320e2
|
|
| MD5 |
cb2b1afefb166688b23615a055bc9674
|
|
| BLAKE2b-256 |
6c95fc0f3f01a2c616763afcceb7f5af89dabc1c3b334c54f514de8424b0ed14
|
Provenance
The following attestation bundles were made for dynamic_config_nacos-0.1.3.tar.gz:
Publisher:
publish.yml on franktz/dynamic-config-nacos
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dynamic_config_nacos-0.1.3.tar.gz -
Subject digest:
631c772bafd5395bfa6b6ae95a26402205f2dbc3f1094ded3cdbc8a38fe320e2 - Sigstore transparency entry: 1178415760
- Sigstore integration time:
-
Permalink:
franktz/dynamic-config-nacos@4cf0993053e4ca52f3b86a591a731ade35a6acde -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/franktz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4cf0993053e4ca52f3b86a591a731ade35a6acde -
Trigger Event:
release
-
Statement type:
File details
Details for the file dynamic_config_nacos-0.1.3-py3-none-any.whl.
File metadata
- Download URL: dynamic_config_nacos-0.1.3-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b283629ec813759823138e7a0d67f798e480e98f078b786583e52f5bc457512
|
|
| MD5 |
d3469a00735ac715cba916433fcde37b
|
|
| BLAKE2b-256 |
215e8c0ede793d038acfd47ed94b357e7f7574f5df0b36eb51387575a4f750ac
|
Provenance
The following attestation bundles were made for dynamic_config_nacos-0.1.3-py3-none-any.whl:
Publisher:
publish.yml on franktz/dynamic-config-nacos
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dynamic_config_nacos-0.1.3-py3-none-any.whl -
Subject digest:
3b283629ec813759823138e7a0d67f798e480e98f078b786583e52f5bc457512 - Sigstore transparency entry: 1178415770
- Sigstore integration time:
-
Permalink:
franktz/dynamic-config-nacos@4cf0993053e4ca52f3b86a591a731ade35a6acde -
Branch / Tag:
refs/tags/v0.1.3 - Owner: https://github.com/franktz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@4cf0993053e4ca52f3b86a591a731ade35a6acde -
Trigger Event:
release
-
Statement type: