ZConfig: Structured Configuration Library
ZConfig is a Python library designed for structured, schema-driven configuration, particularly useful for complex applications and logging. It employs an XML-based schema language to define the allowable structure and content of configuration documents, including custom data conversion routines written in Python. This approach offers greater flexibility and validation capabilities compared to Python's standard `ConfigParser` module. ZConfig is currently at version 4.3 and is actively maintained by the Zope Foundation.
Common errors
-
ZConfig.ZConfigError: Could not locate component 'some.package.component_name'
cause ZConfig could not find the specified schema component. This typically happens if the `component.xml` file is not in the correct Python package location or the package itself is not discoverable.fixVerify that the Python package containing the schema component is correctly installed and accessible on `PYTHONPATH`. Ensure `component.xml` is present at the root of the specified Python package. -
TypeError: configureLoggers() missing 1 required positional argument: 'config'
cause The `configureLoggers` function was called without providing the XML configuration string as its argument.fixPass a valid XML string containing the logging configuration to the `configureLoggers` function. For example: `configureLoggers('<logger>...</logger>')`. -
ZConfig.ZConfigError: Invalid configuration format: syntax error at line X, column Y
cause The provided XML schema or configuration file has a syntax error or does not conform to the expected ZConfig XML structure.fixCarefully review the XML configuration or schema file for well-formedness. Check for unclosed tags, incorrect attributes, or other XML parsing issues. Refer to ZConfig documentation for correct schema syntax. -
ERROR: Package 'zconfig' requires Python '>=3.10' but the running Python is '3.9.x'
cause You are attempting to install `zconfig` version 4.3 (or a similar recent version) using a Python interpreter that does not meet its minimum version requirement.fixUpgrade your Python environment to version 3.10 or higher. Alternatively, install an older version of `zconfig` that is compatible with your current Python version, e.g., `pip install 'zconfig<4.0'`.
Warnings
- breaking ZConfig versions frequently drop support for older Python versions. For example, version 4.0 dropped Python 2.7, 3.5, 3.6; version 4.2 dropped 3.7, 3.8; and version 4.3 dropped 3.9. Always check `requires_python` on PyPI for compatibility.
- breaking Schema component loading underwent a significant change in version 3.5. Older versions searched directories, while 3.5+ strictly requires schema components to be located within Python packages and discoverable via standard import mechanisms (`component.xml` at package root).
Install
-
pip install zconfig
Imports
- configureLoggers
from ZConfig import configureLoggers
- loadSchema
from ZConfig import loadSchema
- loadConfig
from ZConfig import loadConfig
Quickstart
from ZConfig import configureLoggers
import logging
# Define a simple ZConfig XML for logging
log_config = '''
<logger>
level INFO
<logfile>
PATH STDOUT
format %(levelname)s %(name)s %(message)s
</logfile>
</logger>
'''
# Configure the Python logging framework using ZConfig
configureLoggers(log_config)
# Get a logger and test it
logger = logging.getLogger()
logger.info('An info message')
logger.debug('A debug message') # This will not show as level is INFO