'Python libsass sass.complie() not compiling sass

I am having a problem getting libsass to compile SASS files. I can get it to compile SCSS files just fine.

I have a Flask application and I've installed libsass and I am using the libsass binding sass. It works well but is missing some functionality - it can't compile SASS but it can compile SCSS. Here's what I did:

In my applicaiton.py file I have

from flask import Flask
import sass

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    sass.compile(dirname=('client/sass', 'client/css'), source_comments=True)
    app.run()

My two test files are test_scss.scss, which works:

$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}

And test_sass.sass:

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color

The documentation says sass.complie can take in SASS code but I can only get it to take SCSS code. That is, when I run my application, only the .scss file gets compile to CSS. The directory structure of my applicaiton after sass.compile runs is:

  • client
    • css
      • app.css
      • test_scss.css
    • lib
    • sass
      • test_sass.sass
      • test_scss.scss
  • server
    • templates
    • application.py

Someone suggested that I just rename the SASS file to something with a .scss extension. Like test_sass.scss:'

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color

However, when I do this, I just get the following error:

src/web-internal/server/applicaiton.py", line 8, in <module>
    sass.compile(dirname=('client/sass', 'client/css'), source_comments=True)
  File "/usr/local/lib/python3.4/site-packages/sass.py", line 516, in compile
    raise CompileError(v)
sass.CompileError: b"Error: top-level variable binding must be terminated by ';'\n        on line 2 of src/web-internal/server/../client/sass/test_sass.scss\n>> $primary-color: #333\n   ^\n"

I'd like to use SASS, and would perfer not to settle for SCSS. Does anyone have any suggestions? I feel like there is something in the sass documentation that I am missunderstanding.



Solution 1:[1]

I never got this working with sass.compile but I found a work around.

...

if __name__ == '__main__':
    # Start sassc with --watch
    sass_cmd = subprocess.Popen(['sass', '--watch', 'client/sass' + ':' + 'client/css'])
    ... 

Solution 2:[2]

The Python libsass package's compile() method supports the indented kwarg to tell the compiler if the given input is indented .sass or regular bracketed .scss.

If you are using indented .sass as your input, just use it like this:

indented_sass_string = """

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color
"""

css = sass.compile(string=indented_sass_string, indented=True)

I assume that this will work if you are passing entire directories. I am not sure what happens if you try to compile an entire directory which has a mix of .sass and .scss files.

See the docs: https://sass.github.io/libsass-python/sass.html#sass.compile

Solution 3:[3]

If you want to compile any file, then you can do it like this

import sass
sass.compile(dirname=("client/sass", "client/css"))

libsass-python documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mike
Solution 2 kbuilds
Solution 3 Al Mahdi