Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] The error for not having Jinja installed is confusing #213

Open
shaib opened this issue Jul 15, 2023 · 0 comments
Open

[Bug] The error for not having Jinja installed is confusing #213

shaib opened this issue Jul 15, 2023 · 0 comments

Comments

@shaib
Copy link

shaib commented Jul 15, 2023

Describe the bug
If you start out with Sanic and install sanic[ext], then try to use templating before installing Jinja2, you get a confusing error which may make you think that Sanic itself is broken.

I had a fairly trivial example, for a template using a request parameter
(irrelevant lines removed)

from sanic import Sanic

app = Sanic("ShaiApp")

@app.get("/room/<room_name>")
@app.ext.template("room.html")
async def handler(request: Request):
    return {"room_name": room_name}

This was in server.py. Trying to run it without Jinja installed yielded:

$ sanic server:app 
Traceback (most recent call last):
  File "/home/shai/.virtualenvs/sanic3.10/bin/sanic", line 8, in <module>
    sys.exit(main())
  File "/home/shai/.virtualenvs/sanic3.10/lib/python3.10/site-packages/sanic/__main__.py", line 12, in main
    cli.run(args)
  File "/home/shai/.virtualenvs/sanic3.10/lib/python3.10/site-packages/sanic/cli/app.py", line 106, in run
    app = self._get_app(app_loader)
  File "/home/shai/.virtualenvs/sanic3.10/lib/python3.10/site-packages/sanic/cli/app.py", line 175, in _get_app
    app = app_loader.load()
  File "/home/shai/.virtualenvs/sanic3.10/lib/python3.10/site-packages/sanic/worker/loader.py", line 78, in load
    module = import_module(self.module_name)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/shai/sanic/server.py", line 13, in <module>
    @app.ext.template("room.html")
  File "/home/shai/.virtualenvs/sanic3.10/lib/python3.10/site-packages/sanic_ext/bootstrap.py", line 210, in template
    return self.templating.template(template_name, **kwargs)
AttributeError: 'Extend' object has no attribute 'templating'. Did you mean: 'template'?

Expected behavior
I'd expect an error saying something like "you need a templating library installed". Something to make it clear that it's me doing something wrong, not the framework.

Environment (please complete the following information):

  • OS: Debian testing (currently trixie)
  • Version 23.3.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant