{"id":6371,"library":"gin-config","title":"Gin-Config","description":"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.","status":"active","version":"0.5.0","language":"en","source_language":"en","source_url":"https://github.com/google/gin-config","tags":["configuration","dependency injection","machine learning","experiment management"],"install":[{"cmd":"pip install gin-config","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"gin","correct":"import gin"},{"note":"The decorator is usually accessed via the top-level 'gin' import.","wrong":"from gin_config import configurable","symbol":"configurable","correct":"@gin.configurable"},{"note":"The decorator is usually accessed via the top-level 'gin' import.","wrong":"from gin_config import register","symbol":"register","correct":"@gin.register"},{"symbol":"parse_config_file","correct":"gin.parse_config_file('config.gin')"},{"symbol":"parse_config_files_and_bindings","correct":"gin.parse_config_files_and_bindings(gin_files, gin_params)"},{"symbol":"query_parameter","correct":"gin.query_parameter('my_function.param_name')"},{"symbol":"REQUIRED","correct":"def my_function(param=gin.REQUIRED):"},{"symbol":"operative_config_str","correct":"config_string = gin.operative_config_str()"}],"quickstart":{"code":"import gin\n\n# 1. Define a configurable function or class\n@gin.configurable\ndef greet(name='World', greeting='Hello'):\n    return f\"{greeting}, {name}!\"\n\n# 2. Create a config file (e.g., config.gin)\n# Save this content to a file named 'config.gin':\n# greet.name = 'Gin-Config User'\n# greet.greeting = 'Hi there'\n\n# 3. In your Python code, parse the config file\n# For demonstration, we'll use gin.parse_config_string\n# In a real application, you'd use gin.parse_config_file('config.gin')\ngin_config_content = \"\"\"\ngreet.name = 'Gin-Config User'\ngreet.greeting = 'Hi there'\n\"\"\"\ngin.parse_config_string(gin_config_content)\n\n# 4. Call the configurable function\n# Gin will automatically inject parameters from the config\nresult = greet()\nprint(result)\n\n# You can still override configured values by passing arguments directly\nresult_override = greet(name='Developer')\nprint(result_override)\n\n# Clear configurations (useful for testing or multiple configurations)\ngin.clear_config()\n\n# Demonstrate gin.REQUIRED\n@gin.configurable\ndef show_required(value=gin.REQUIRED):\n    return f\"Required value: {value}\"\n\n# Try to call without configuring or providing 'value'\ntry:\n    show_required()\nexcept ValueError as e:\n    print(f\"Expected error for missing required parameter: {e}\")\n\n# Configure and call\ngin.parse_config_string(\"show_required.value = 42\")\nprint(show_required())\n\n","lang":"python","description":"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."},"warnings":[{"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').","message":"Confusion 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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Choose the appropriate decorator based on whether you want direct calls to respect Gin's configuration or only indirect/referenced calls.","message":"Distinction 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Handling 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Arithmetic 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Pylint 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'.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z"}