Registry / ai-ml / gin-config

gin-config

JSON →
library0.5.0pypypi✓ verified 91d ago

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-config
INSTALL
IMPORT
SIG · GIN-CONFIG
G
gin-config
ai-mlpythonv0.5.0
Install
1.6s avg
Import
68ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.071s · 18.2MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 1.6s · import 0.066s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

gin
✓ import gin
configurable
✓ @gin.configurable
✗ from gin_config import configurable
The decorator is usually accessed via the top-level 'gin' import.
register
✓ @gin.register
✗ from gin_config import register
The decorator is usually accessed via the top-level 'gin' import.
parse_config_file
✓ gin.parse_config_file('config.gin')
parse_config_files_and_bindings
✓ gin.parse_config_files_and_bindings(gin_files, gin_params)
query_parameter
✓ gin.query_parameter('my_function.param_name')
REQUIRED
✓ def my_function(param=gin.REQUIRED):
operative_config_str
✓ config_string = gin.operative_config_str()

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.

import gin # 1. Define a configurable function or class @gin.configurable def greet(name='World', greeting='Hello'): return f"{greeting}, {name}!" # 2. Create a config file (e.g., config.gin) # Save this content to a file named 'config.gin': # greet.name = 'Gin-Config User' # greet.greeting = 'Hi there' # 3. In your Python code, parse the config file # For demonstration, we'll use gin.parse_config_string # In a real application, you'd use gin.parse_config_file('config.gin') gin_config_content = """ greet.name = 'Gin-Config User' greet.greeting = 'Hi there' """ gin.parse_config_string(gin_config_content) # 4. Call the configurable function # Gin will automatically inject parameters from the config result = greet() print(result) # You can still override configured values by passing arguments directly result_override = greet(name='Developer') print(result_override) # Clear configurations (useful for testing or multiple configurations) gin.clear_config() # Demonstrate gin.REQUIRED @gin.configurable def show_required(value=gin.REQUIRED): return f"Required value: {value}" # Try to call without configuring or providing 'value' try: show_required() except ValueError as e: print(f"Expected error for missing required parameter: {e}") # Configure and call gin.parse_config_string("show_required.value = 42") print(show_required())
Debug
Known issues
gotchaConfusion with 'gin-gonic/gin' (Go framework). There is a popular Go web framework also named 'Gin'. Ensure you are installing and referencing `gin-config` (for Python) and not the Go project.
fix
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').
affects: All versions
gotchaDistinction between `@gin.configurable` and `@gin.register`. Functions decorated with `@gin.configurable` will have their parameters overridden by Gin configurations even when called directly from other Python code. Functions with `@gin.register` will *not* have their parameters overridden when called directly; configurations only apply when they are referenced using the `@some_name` syntax within config files or other Gin contexts.
fix
Choose the appropriate decorator based on whether you want direct calls to respect Gin's configuration or only indirect/referenced calls.
affects: All versions
gotchaHandling required parameters with `gin.REQUIRED`. While `gin.REQUIRED` can be used as a default value in a function signature, it is generally more flexible and often preferred to provide `gin.REQUIRED` at the call site if the function might be called multiple times with different requirements.
fix
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.
affects: All versions
gotchaArithmetic expressions are not supported in Gin config files. While the configuration syntax is Python-like and supports literals, comments, and line continuation, it does not evaluate arithmetic expressions.
fix
Pre-calculate any necessary arithmetic values in your Python code before parsing or provide the final numeric literals directly in the `.gin` configuration file.
affects: All versions
gotchaPylint warnings for unused imports with configurable items. When using `@gin.configurable` or `@gin.register`, you must import all functions/classes that might be configured, even if only one is selected at runtime via the config. This can lead to `pylint` flagging 'unused imports'.
fix
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.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'gin'
The `gin-config` package is installed, but users attempt to import it using `import gin` instead of `import gin.config` or by importing specific submodules like `gin.tf` or `gin.torch` when those submodules are not part of the installed `gin-config` or have been moved/removed in newer versions.
fix
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.
ValueError: No configurable matching 'my_function'
This error occurs when `gin.parse_config_file()` or `gin.parse_config_string()` is called, but the configurable function or class referenced in the configuration (e.g., 'my_function' in `my_function.param = value`) has not yet been defined with `@gin.configurable` or imported into the current execution scope.
fix
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.
ValueError: Configurable 'FunctionName' doesn't have a parameter named 'param_name'
This error indicates a mismatch between the configuration file and the Python code. A parameter is specified in the `.gin` config file for a configurable function or class, but that parameter does not exist in the definition of the function or class. This commonly happens after refactoring code where parameters are renamed or removed, but the configuration file is not updated.
fix
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.
RuntimeError: Attempted to modify locked Gin config.
The `gin-config` system has been 'finalized' by calling `gin.finalize()` to prevent further modifications, but a subsequent attempt is made to bind or re-bind parameters.
fix
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`.
Missing required parameter: [parameter_name]
A configurable function or class has a parameter explicitly marked as `gin.REQUIRED` (e.g., `param=gin.REQUIRED`), but no value for this parameter has been provided either in the call site, the gin configuration, or a default binding.
fix
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.
Upgrade
Version history
0.5.0latest on PyPI · released Nov 3, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
gin-config — pip install gin-config · libregistry