miracle2k / flask-assets

Flask webassets integration.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting AttributeError when using with Flask-Script 2.0.5 and webassets 0.11.1

eliasdorneles opened this issue · comments

I followed the instructions to use Flask-Script, but I'm getting the following error when trying to run python manage.py assets check:

$ python manage.py assets check
Checking asset: all.js
Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    manager.run()
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/flask_assets.py", line 431, in run
    return impl.main(args)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/script.py", line 557, in main
    return self.run_with_argv(argv)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/script.py", line 549, in run_with_argv
    return self.run_with_ns(ns)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/script.py", line 539, in run_with_ns
    return cmd.invoke(ns.command, args)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/script.py", line 373, in invoke
    return function(**args)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/script.py", line 323, in __call__
    if updater.needs_rebuild(bundle, self.environment):
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/updater.py", line 174, in needs_rebuild
    self.check_timestamps(bundle, ctx)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/updater.py", line 123, in check_timestamps
    resolved_output = bundle.resolve_output(ctx)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/webassets/bundle.py", line 326, in resolve_output
    output = ctx.resolver.resolve_output_to_path(ctx, self.output, self)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/flask_assets.py", line 201, in resolve_output_to_path
    directory, rel_path, endpoint = self.split_prefix(ctx, target)
  File "/home/elias/.virtualenvs/tmp-e8a1ce65c4ea7129/local/lib/python2.7/site-packages/flask_assets.py", line 152, in split_prefix
    app = ctx.environment._app
AttributeError: 'Environment' object has no attribute 'environment'

I've just reproduced it in a temporary virtualenv, here are the steps:

  1. Install latest stable flask, flask-script and flask-assets:
pip install flask flask-script flask-assets

Here I get the following:

$ pip freeze
Flask==0.10.1
Flask-Assets==0.11
Flask-Script==2.0.5
itsdangerous==0.24
Jinja2==2.8
MarkupSafe==0.23
webassets==0.11.1
Werkzeug==0.11.3
wheel==0.24.0
  1. Create a minimal app adding a manage.py for Flask-Script and importing the assets environment -- here is what I have:
$ cat myapp.py
from flask import Flask, render_template
from flask.ext.assets import Environment, Bundle


app = Flask(__name__)
assets_env = Environment(app)

js = Bundle('base.js', 'widgets.js',
            output='all.js')
assets_env.register('js_all', js)


@app.route("/")
def index():
    return render_template('index.html')


if __name__ == "__main__":
    app.run()

$ cat manage.py 
from flask.ext.script import Manager
from flask.ext.assets import ManageAssets
from myapp import app, assets_env

manager = Manager(app)
manager.add_command("assets", ManageAssets(assets_env))

if __name__ == "__main__":
    manager.run()

$ cat static/base.js 
console.log('base.js')

$ cat static/widgets.js 
console.log('widgets.js')

$ cat templates/index.html 
{% assets "js_all" %}
<script type="text/javascript" src="{{ ASSET_URL }}"></script>
{% endassets %}
  1. Run python manage.py assets check and you'll get the error from above.

Am I doing something wrong?

Try adding this line: app.jinja_env.assets_environment.environment = app.jinja_env.assets_environment

It's an issue that's been fixed in the master branch but the flask-assets guys don't want to push 0.12 yet, it seems.

Closing because of age. Hopefully the new 2.0 version does not have this.