SQLGlot

raw JSON →
25.25.2.dev9 verified Mon Apr 27 auth: no python

SQLGlot is a no-dependency SQL parser, transpiler, and optimizer written in Python. It can parse a wide variety of SQL dialects, transpile between them, and produce formatted SQL. Current version is 25.25.2.dev9 (development release; stable releases follow semantic versioning and are frequent).

pip install acryl-sqlglot
error ModuleNotFoundError: No module named 'acryl_sqlglot'
cause Trying to import the PyPI package name directly.
fix
Install with 'pip install acryl-sqlglot' but import as 'import sqlglot'.
error ImportError: cannot import name 'parse' from 'sqlglot'
cause Attempting to import a non-existent function 'parse'.
fix
Use 'from sqlglot import parse_one' (not 'parse').
gotcha Import from 'sqlglot', NOT 'acryl_sqlglot'. The PyPI package is 'acryl-sqlglot' but the Python module is 'sqlglot'.
fix Use 'import sqlglot' or 'from sqlglot import ...'.
breaking In older versions (pre-10.0), the dialect API was different. The 'dialect' argument in parse_one was replaced with explicit dialect classes.
fix Use dialect classes like sqlglot.dialects.mysql.MySQL.

Parse SQL, transpile between dialects, and format.

import sqlglot

# Parse and transpile SQL
result = sqlglot.transpile("SELECT * FROM table", read='mysql', write='postgres')
print(result[0])  # Output: SELECT * FROM "table"

# Pretty print
pretty = sqlglot.transpile("SELECT a,b FROM foo", pretty=True)
print(pretty[0])