crudini

raw JSON →
0.9.6 verified Fri May 01 auth: no python

A command-line utility for manipulating INI configuration files. It can get, set, delete, and merge values in sections. Current version: 0.9.6, released 2021-03. Maintenance-focused, low release cadence.

pip install crudini
error ImportError: No module named crudini
cause crudini is not a Python importable module; it is a command-line script.
fix
Call it via subprocess.run(['crudini', ...]) or install it via pip and invoke as crudini in shell.
error ERROR: [section] parameter 'key' does not exist
cause Trying to --get a key that doesn't exist in the section.
fix
Verify the key exists or use --set to add it first.
error Failed to open file: /path/to/file.ini
cause File does not exist or permissions issue.
fix
Check file path and permissions; use --existing=file if you want crudini to require the file.
error crudini: error: argument --output: expected one argument
cause Missing value for --output flag.
fix
Provide a filename after --output, e.g., --output new.ini.
gotcha crudini is not importable as a Python module. It is a CLI tool. Do not try `from crudini import something`.
fix Use subprocess.run(['crudini', ...]) to call it from Python.
gotcha If the ini file doesn't exist and you use --set without --existing, crudini will create the file. The --existing flag prevents this.
fix Use --existing=file if you want to enforce that the file already exists.
gotcha crudini modifies files in-place; it does not output to stdout by default. Use --output or redirection carefully.
fix Use `--output` flag or shell redirection to avoid unintended file modifications.
breaking In version 0.9, the --get command now maintains the case of parameter names. Previously it was lowercased.
fix Ensure your script does not expect lowercased output from --get.

Use subprocess to invoke crudini; it has no Python importable API.

import subprocess
import tempfile
import os

content = """[section]
key = value
"""
with tempfile.NamedTemporaryFile(mode='w', suffix='.ini', delete=False) as f:
    f.write(content)
    fname = f.name

try:
    subprocess.run(['crudini', '--get', fname, 'section', 'key'], check=True, capture_output=True, text=True)
    print('Get succeeds')
except subprocess.CalledProcessError as e:
    print('Error:', e.stderr)
finally:
    os.unlink(fname)