pyconf-ezload is a simple Python library for loading configurations from different sources such as environment variables and configuration files (JSON/YAML). It supports flexible configurations with features like default values, required keys, and filtering based on key prefixes.
To install the package, you can use pip:
pip install pyconf-ezloadAlternatively, you can install it from the source using poetry:
poetry installYou can load configuration directly from environment variables, optionally filtering by key prefix, providing default values, or marking certain variables as required.
from pyconfig_loader import ConfigLoader
# Example of loading configuration from environment variables
loader = ConfigLoader("env")
# Load with required keys and default values
config = loader.load_from_env(
required_keys=["DB_USERNAME", "DB_PASSWORD"],
default_values={"DB_PASSWORD": "default_pass"},
key_prefix="DB_"
)
print(config)This would output a dictionary like:
{
'DB_USERNAME': 'admin', # from env
'DB_PASSWORD': 'default_pass' # default value as it's missing in the environment
}You can also load configuration from a file, supporting both JSON and YAML formats.
{
"DB_USERNAME": "admin",
"DB_PASSWORD": "secret"
}DB_USERNAME: admin
DB_PASSWORD: secretfrom pyconfig_loader import ConfigLoader
# Example of loading configuration from a JSON or YAML file
loader = ConfigLoader("path/to/config.json") # Or "path/to/config.yaml"
config = loader.load()
print(config)By default, if required environment variables are missing, a ValueError will be raised. You can modify this behavior with the raise_on_missing argument.
config = loader.load_from_env(
required_keys=["DB_USERNAME", "DB_PASSWORD"],
raise_on_missing=False # Will not raise an error if keys are missing
)- Prefix Filtering: You can filter environment variables based on a prefix (e.g., load only variables starting with
DB_). - Default Values: Specify default values for missing environment variables.
- Required Keys: Define which environment variables are required to exist in the configuration, and optionally set default values for them.
config = loader.load_from_env(
required_keys=["API_KEY", "DB_PASSWORD"],
default_values={"DB_PASSWORD": "default_pass"},
key_prefix="APP_"
)To contribute to pyconf-ezload, you can clone the repository and use poetry to manage dependencies and run tests.
git clone https://github.com/yourusername/pyconf-ezload.git
cd pyconf-ezload
poetry install
poetry run pytest # To run testsThis project is licensed under the MIT License - see the LICENSE file for details.