Emerging
May 28, 20261
50%
Py-SQL-cleaner: New CLI Tool for Formatting SQL in Python Code
Py-sql-cleaner is a new CLI tool that formats, extracts, and manages SQL queries embedded in Python triple-quoted strings. Built on SQLGlot, it supports multiple database dialects and includes safety features that preserve runtime behavior by skipping dynamic content like f-strings and templates.
Quick Facts
Who
Python developers
What
Released py-sql-cleaner CLI tool
When
2026-05-28
Where
GitHub
- Released py-sql-cleaner CLI tool
- Tool formats SQL embedded in Python strings
- Tool extracts SQL into external .sql files
- Tool replaces embedded SQL strings with file references
- Tool detects common SQL variable names
A new open-source command-line tool called py-sql-cleaner has been released to help Python developers format and manage SQL queries embedded directly in their code. The tool addresses a common challenge in Python codebases where lengthy SQL statements are written as triple-quoted strings within Python files.
Py-sql-cleaner is built on top of SQLGlot, a SQL parser and formatter, and provides several core functionalities. Developers can use it to format SQL embedded in Python strings in place, extract SQL into separate external .sql files, replace embedded SQL strings with file references, and detect common SQL variable naming patterns such as sql, query, and *_sql or *_query suffixes. The tool also supports multiple database dialects including generic, MySQL, PostgreSQL, and Redshift, allowing users to format SQL according to specific database conventions.
The tool emphasizes safety and preserves runtime behavior by default. It automatically skips formatting unsafe blocks such as f-strings, Jinja-like templates, and runtime placeholders, which could change program execution if modified. The MVP targets Python triple-quoted strings rather than single-line SQL strings. Py-sql-cleaner operates as a static analysis tool only—it does not connect to databases, execute SQL, or perform exhaustive database validation.
Key features include a list command to identify embedded SQL blocks, a dry-run mode for previewing changes before applying them, and a check mode designed for continuous integration pipelines. Installation is straightforward via PyPI using pip or pipx, with the tool also available as release archives and wheels on GitHub. Users can also run it without installing via uvx. The formatting output is controlled by SQLGlot's parser and formatter, which may evolve as SQLGlot itself changes.
Topics
Why This Matters
Py-sql-cleaner solves a practical problem for Python developers who embed SQL queries directly in code: it enables consistent SQL formatting, code organization through extraction to separate files, and easier maintenance of large codebases. The tool's emphasis on safety—automatically skipping dynamic content like f-strings—means developers can use it confidently without risking runtime errors. For teams adopting linting and code quality standards, the CI/CD-ready check mode provides automated validation, reducing manual code review overhead.
Timeline & Sources
May 28, 2026
WirePy-sql-cleaner MVP released and announced on Hacker News