Gin provides a lightweight configuration framework for Python, based on dependency injection. Functions or classes can be decorated with @gin.configurable, allowing default parameter values to be supplied from a config file (or passed via the command line) using a simple but powerful syntax. This removes the need to define and maintain configuration objects or write boilerplate parameter plumbing and factory code, while often dramatically expanding a project's flexibility and configurability. It is particularly well suited for machine learning experiments. It is currently at version 0.5.0 and is actively maintained by Google.
pip install gin-configVerified import paths — ran on the pinned version, not inferred.
To get started with Gin-Config, you define functions or classes that you want to make configurable by decorating them with `@gin.configurable`. You then create a `.gin` configuration file where you specify parameter bindings using a `function_name.parameter_name = value` syntax. Finally, you parse this configuration file in your Python application using `gin.parse_config_file()`, and Gin-Config automatically injects the configured values when the decorated functions or classes are called.
Always use `pip install gin-config` for the Python library and refer to the official GitHub repository `google/gin-config` for documentation. When searching, be specific (e.g., 'gin-config python').
Choose the appropriate decorator based on whether you want direct calls to respect Gin's configuration or only indirect/referenced calls.
Prefer `my_function(param=gin.REQUIRED)` when defining configurable functions if a parameter must always be supplied by Gin or the caller. Consider passing `gin.REQUIRED` at the call site for greater flexibility in some scenarios.
Pre-calculate any necessary arithmetic values in your Python code before parsing or provide the final numeric literals directly in the `.gin` configuration file.
Suppress the `unused-import` pylint warning for modules containing multiple configurable items, or structure your code to minimize the number of configurable items within a single module.
Ensure `gin-config` is installed (`pip install gin-config`). The primary module is `gin.config`, so use `import gin.config` or decorate functions/classes with `@gin.configurable`. If trying to import `gin.tf` or `gin.torch`, verify that the `tensorflow` or `torch` integrations for `gin-config` are correctly installed and that the modules exist in your current `gin-config` version. Often, just importing `gin.config` is sufficient, or installing `gin-config` as `pip install -U gin-config` for the latest version.
Ensure that all functions and classes that are intended to be configurable by `gin-config` are decorated with `@gin.configurable` and that their containing modules are imported *before* `gin.parse_config_file()` or `gin.parse_config_string()` is called.
Update the `.gin` configuration file to reflect the current parameters of the configurable function or class. Alternatively, if the parameter is optional and no longer needed, remove its entry from the configuration file. If the parameter genuinely no longer exists in the code and you are loading an older config, consider using `skip_unknown=True` with `gin.parse_config_file()` if applicable, though it's generally better to align config and code.
Ensure all `gin.bind_parameter()` calls and `gin.parse_config_file()` (or `parse_config_string()`) calls occur before `gin.finalize()`. If modifications are truly needed after finalization, you can temporarily unlock the configuration using a context manager: `with gin.unlock_config(): # modify config here`.
Provide a value for the missing required parameter. This can be done in the `.gin` configuration file (e.g., `MyConfigurable.parameter_name = 'value'`), by calling `gin.bind_parameter('MyConfigurable.parameter_name', 'value')`, or directly when calling the configurable function or instantiating the class.No dependency data recorded yet.