Django Sass压缩器django_libsass. Sass编译器:找不到命令

fwzugrvs  于 2022-11-26  发布在  Go
关注(0)|答案(5)|浏览(130)

我正在使用Django压缩过滤器作为Wagtail的一部分(Django变体CMS具有超酷的UI)。环境是Wagtail 0.2 + Python 2.7 + Django 1.6 + Virtualenv + FastCGI + Apache共享主机。
问题发生在尝试访问CMS的管理/登录页面时。Django显示一个错误渲染模板

Error during template rendering

In template /home/username/env/lib/python2.7/site-packages/wagtail/wagtailadmin/templates/wagtailadmin/skeleton.html, error at line 20
/bin/sh: django_libsass.SassCompiler: command not found

skeleton.html的第20行是:

<!doctype html>
    {% load compress %}
    <!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"  lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <![endif]-->
    <!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"  lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <![endif]-->
    <!--[if IE 8]>         <html class="no-js lt-ie9"  lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <![endif]-->
    <!--[if gt IE 8]><!--> <html class="no-js"  lang="{{ LANGUAGE_CODE|default:"en-gb" }}"> <!--<![endif]-->
        <title>Wagtail - {% block titletag %}{% endblock %}</title>
        <meta name="description" content="" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />

        <script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>

        {% block css %}{# Block defined for timing breakdowns in django debug toolbar - not expected to be overridden #}
            <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" />
            <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Bitter:400,700" /> 
19          
20          {% compress css %}
21              <link rel="stylesheet" href="{{ STATIC_URL }}wagtailadmin/scss/normalize.css" />
                <link rel="stylesheet" href="{{ STATIC_URL }}wagtailadmin/scss/vendor/jquery-ui/jquery-ui-1.10.3.verdant.css" />
                <link rel="stylesheet" href="{{ STATIC_URL }}wagtailadmin/scss/vendor/jquery.timepicker.css" />
                <link rel="stylesheet" href="{{ STATIC_URL }}wagtailadmin/scss/core.scss" type="text/x-scss" />
            {% endcompress %}

            {% block extra_css %}{% endblock %}
        {% endblock %}
    </head>
    <body class="{% block bodyclass %}{% endblock %} {% if messages %}has-messages{% endif %}">

预编译器在我的www.example.com中settings.py,DEBUG设置为True:

COMPRESS_ENABLED = True

COMPRESS_PRECOMPILERS = ( 
     ('text/x-scss', 'django_libsass.SassCompiler'), 
)

试验与错误#1

我已尝试更改为:

('text/x-scss', '/home/username/env/lib/python2.7/site-packages/django_libsass {infile} {outfile}')

但这会导致字典更新序列元素#0错误。
我安装了django_libsass & compressor,也尝试了pip install libsassngm install lesscpip install sass,旋转DEBUG = False,添加COMPRESSOR_OFFLINE,添加COMPRESSOR_ENABLED,就像其他类似问题中建议的那样。运行manage.py compress返回相同的错误。
我重新检查了一下,site-packages和django_libsass确实在我的sys.path上
可以在~/env/lib/python2.7/site-packages/django_libsass.py中找到SassCompiler

试验与错误#2

重新检查sass是否已安装并位于我的路径上。
将代码更改为:

COMPRESS_PRECOMPILERS = ( 
     ('text/x-scss', 'sass --scss {infile} {outfile}'), 
 )

返回错误:

Exception Type: FilterError
Exception Value:    
/bin/sh: sass: command not found

试验与错误#3

阅读此feedly issue后,尝试pip install compass,但没有效果

试验与错误#4

在gasman的评论之后,我运行了python manage.py shell,并试图导入SassCompiler。它工作时没有错误。

Python 2.7.6 (default, Nov 11 2013, 18:34:29) 
    [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> from django_libsass import SassCompiler
    >>>

完整追溯

如果这个问题太长了,我很抱歉。

Traceback:
File "/home/username/env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  139.                 response = response.render()
File "/home/username/env/lib/python2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/home/username/env/lib/python2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/username/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/username/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/home/username/env/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/home/username/env/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/home/username/env/lib/python2.7/site-packages/compressor/templatetags/compress.py" in render
  147.         return self.render_compressed(context, self.kind, self.mode, forced=forced)
File "/home/username/env/lib/python2.7/site-packages/compressor/templatetags/compress.py" in render_compressed
  107.             rendered_output = self.render_output(compressor, mode, forced=forced)
File "/home/username/env/lib/python2.7/site-packages/compressor/templatetags/compress.py" in render_output
  119.         return compressor.output(mode, forced=forced)
File "/home/username/env/lib/python2.7/site-packages/compressor/css.py" in output
  51.                     ret.append(subnode.output(*args, **kwargs))
File "/home/username/env/lib/python2.7/site-packages/compressor/css.py" in output
  53.         return super(CssCompressor, self).output(*args, **kwargs)
File "/home/username/env/lib/python2.7/site-packages/compressor/base.py" in output
  246.         content = self.filter_input(forced)
File "/home/username/env/lib/python2.7/site-packages/compressor/base.py" in filter_input
  194.         for hunk in self.hunks(forced):
File "/home/username/env/lib/python2.7/site-packages/compressor/base.py" in hunks
  169.                 precompiled, value = self.precompile(value, **options)
File "/home/username/env/lib/python2.7/site-packages/compressor/base.py" in precompile
  226.                                 **kwargs)
File "/home/username/env/lib/python2.7/site-packages/django_libsass.py" in input
  51.             return compile(filename=self.filename)
File "/home/username/env/lib/python2.7/site-packages/django_libsass.py" in compile
  41.     return sass.compile(**kwargs)

Exception Type: AttributeError at /admin/login/
Exception Value: 'module' object has no attribute 'compile'
olhwl3o2

olhwl3o21#

(根据要求,重新发布我的评论作为回答...)
原错误:django_libsass.SassCompiler: command not found
(django-compressor通过尝试将其作为shell命令来响应该故障- django_libsass不是可运行的命令,因此这也失败了,给出了这里看到的实际错误。)解决方案是确保django-libsass已安装-它应该显示在pip freeze的输出中。
第二个错误:'module' object has no attribute 'compile'
这意味着安装了另一个包,它定义了一个名为sass的模块,而这个包被加载到了我们想要的位置,从libsass包.解决方法是卸载所有sass相关的包,除了django-libsass和libsass.

tvokkenx

tvokkenx2#

我的Python-Django项目和rvm也有同样的问题,问题是compress预编译器不知道使用哪个rvm,解决方法是在environ中添加ruby路径,告诉预编译器在www.example.com文件中哪里可以找到sasssettings.py:
就像在我的例子中(只需执行which sass来查找路径):

environ['PATH'] = '/Users/username/.rvm/gems/ruby-2.1.5/bin:/Users/username/.rvm/gems/ruby-2.1.5@global/bin:/Users/username/.rvm/rubies/ruby-2.1.5/bin:Users/username/bin'

 COMPRESS_PRECOMPILERS = (
 ('text/x-scss', '/Users/username/.rvm/gems/ruby-2.1.5/bin/sass --scss --trace {infile} {outfile}'),
 )

希望这能帮助到一些人。

91zkwejq

91zkwejq3#

错误

/bin/sh: django_libsass.SassCompiler: command not found

表示Django Compressor正在尝试将django_libsass.SassCompiler作为shell命令运行,但脚本运行失败。
这是因为django_libsass.SassCompiler不是一个可以从命令行运行的有效程序。
如果您还没有安装sass,最好先安装,方法是按照本页上的说明进行操作:http://sass-lang.com/install
然后将代码更改为:

COMPRESS_PRECOMPILERS = ( 
     ('text/x-scss', 'sass --scss {infile} {outfile}'), 
 )

您必须确保sass命令在您的路径上。
从文档(http://django-compressor.readthedocs.org/en/latest/settings/#django.conf.settings.COMPRESS_PRECOMPILERS)中,你的元组的第二部分是:
在每个文件上调用的命令。现代Python字符串格式将被提供给两个占位符{infile}和{outfile},它们在命令字符串中的存在也触发了这些临时文件的实际创建。如果命令字符串中没有给出,Django Compressor将分别使用stdin和stdout来代替。

ctrmrzij

ctrmrzij4#

pip install django_compressor==2.4
pip install django-libsass

这对我来说很有效,也安装了最新的django-libsass。

3htmauhk

3htmauhk5#

pip install django_pyscss将修复您的问题。

相关问题