tini Configuration Parser
tini is a lightweight and straightforward Python library designed for reading and parsing simple .ini and configuration files. It provides a dictionary-like interface for accessing sections and keys, with automatic type conversion for common data types. The current version is 4.0.0, and it follows an infrequent release cadence, with major versions tied to significant API changes or Python version support.
Common errors
-
ModuleNotFoundError: No module named 'tini'
cause The 'tini' library is not installed in your Python environment.fixRun `pip install tini` to install the package. -
AttributeError: 'Ini' object has no attribute 'read'
cause You are using the deprecated `read()` method on an `Ini` object in `tini` version 4.0.0 or later.fixThe method was renamed. Use `config.load()` instead of `config.read()`. -
KeyError: 'some_missing_section'
cause You are attempting to access a configuration section or key that does not exist directly using `config['section']['key']` syntax.fixUse the safe `.get()` method which allows providing a default value: `config.get('some_missing_section', {})` or `config['existing_section'].get('missing_key', 'default_value')`. -
TypeError: argument of type 'int' is not iterable
cause You might be iterating over a configuration value that `tini` auto-converted to an integer or boolean, expecting it to be a string or list.fixIf you intend to parse a string value that `tini` auto-converts (e.g., '123' to 123), explicitly cast it back to a string: `str(config['section']['key'])` before iterating or processing as a string.
Warnings
- breaking The `Ini.read()` and `Ini.save()` methods were renamed to `Ini.load()` and `Ini.dump()` respectively.
- breaking Python 2.7 and Python 3.6 support was dropped.
- gotcha tini performs automatic type conversion (int, float, bool, list) for values it recognizes. If you need the raw string value, you must explicitly convert it.
- gotcha Accessing non-existent sections or keys directly (e.g., `config['missing_section']`) will raise a `KeyError`. Use `.get()` for safer access.
Install
-
pip install tini
Imports
- Ini
from tini import Ini
Quickstart
import os
from tini import Ini
# Create a dummy ini file for demonstration
config_content = """
[server]
host = localhost
port = 8080
ssl = True
[database]
type = postgres
user = admin
password = mysecret
servers = db1,db2,db3
"""
config_file_path = "config.ini"
with open(config_file_path, "w") as f:
f.write(config_content)
# Load the configuration
config = Ini(config_file_path)
# Access values
print(f"Server Host: {config['server']['host']}")
print(f"Server Port: {config['server']['port']} (type: {type(config['server']['port'])})")
print(f"SSL Enabled: {config['server']['ssl']} (type: {type(config['server']['ssl'])})")
print(f"Database User: {config['database']['user']}")
print(f"Database Servers: {config['database']['servers']} (type: {type(config['database']['servers'])})")
# Get a value with a default
print(f"API Key (default None): {config['api'].get('key')}")
# Clean up the dummy file
os.remove(config_file_path)