Sass for Python (libsass)
Libsass is a Python binding for LibSass, the C/C++ port of the original Sass CSS preprocessor. It allows Python developers to compile Sass/SCSS files to CSS without needing a Ruby or Node.js runtime. While the `libsass-python` wrapper is actively maintained and currently at version 0.23.0 (released January 6, 2024), the underlying LibSass C/C++ library reached its official End-of-Life in October 2025 and is no longer receiving updates.
Warnings
- breaking The upstream LibSass C/C++ library, which `libsass-python` wraps, reached its End-of-Life in October 2025 and is no longer maintained. It will not receive feature updates or compatibility with new CSS/Sass features. Users are strongly advised to migrate to Dart Sass or a Python binding for Dart Sass, such as `sass-embedded-python`.
- deprecated Python 2.x support was removed in version 0.22.0.
- deprecated The `sassc` command-line interface, previously shipped with libsass-python, was removed in version 0.22.0.
- gotcha Installation of `libsass` may require a C++ compiler (like GCC, Clang, or MSVC) on your system, especially if pre-built wheels are not available for your specific platform or Python version. It requires a compiler that supports recent C++ standards (C++11 or newer).
- gotcha LibSass has known incompatibilities and differences in behavior compared to Dart Sass (the current reference implementation), particularly regarding `@import` vs `@use`, `/` for division, custom property parsing, and `@extend` rules with complex selectors.
- gotcha The `source_comments` parameter for `sass.compile()` changed its accepted values. Historically, it could take strings like 'none', 'line_numbers', 'map'. As of 0.6.0, it primarily accepts boolean values (`True`/`False`).
Install
-
pip install libsass
Imports
- compile
from sass import compile
- CompileError
from sass import CompileError
Quickstart
import sass
scss_code = """
$primary-color: #337ab7;
.container {
h1 {
color: $primary-color;
}
p {
font-size: 1.2em;
}
}
"""
try:
css_output = sass.compile(string=scss_code, output_style='compressed')
print(css_output)
except sass.CompileError as e:
print(f"Sass compilation error: {e}")