Opened 18 years ago

Closed 15 years ago

Last modified 13 years ago

#3349 closed (fixed)

If an ImportError occurs within some loaders a rather confusing exception is raised.

Reported by: Chris Wagner <cw264701@…> Owned by: nobody
Component: Template system Version: dev
Severity: Keywords:
Cc: me@…, ramusus@…, andrewbadr.etc@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The problem is, when Django attempts to load a custom tag library (in source:django/trunk/django/template/__init__.py, in the get_library() function), it traps all ImportError's, regardless of where they are thrown from:

try:
    mod = __import__(module_name, {}, {}, [''])
except ImportError, e:
    raise InvalidTemplateLibrary, "Could not load template library from %s, %s" % (module_name, e)

An example...

If I have a "view" like so:

def my_view(request):
    return render_to_response('my.tpl', {})

...And a corresponding template:

{% load tag_lib %}
<h1>my template</h1>
...

...Now, here's the tag library tag_lib:

import notthere
...

...Invoking our view, now, will yield a not-very-easy-to-understand (unless you already know the problem) error message:

TemplateSyntaxError at /my_view
'tag_lib' is not a valid tag library: Could not load template library from django.templatetags.tag_lib, No module named notthere
Request Method:     GET
Request URL:        http://localhost:8000/my_view
Exception Type:     TemplateSyntaxError
Exception Value:    'tag_lib' is not a valid tag library: Could not load template library from django.templatetags.tag_lib, No module named notthere
Exception Location: /usr/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/template/defaulttags.py in load, line 692

You may need to look closely to notice that the root of the problem was that our tag library tried to load a module notthere; the problem was not that we tried to load a non-existant tag library.

This is essentially the same message that you will get when trying to load a tag library that is completely non-existant - i.e., if our template were:

{% load not_tag_lib %}
<h1>my template</h1>
...

We would now see the error message:

TemplateSyntaxError at /my_view
'not_tag_lib' is not a valid tag library: Could not load template library from django.templatetags.not_tag_lib, No module named not_tag_lib
...

The type of error message that I would like to see would be one that is not obfuscated and wrapped in a TemplateSyntaxError exception. Such a useful error message will be displayed if some other problem occurs in our tag library; for instance, if our tag library looks like

blablabla
...

then we will be greeted with the slightly-more-decipherable error message,

NameError at /my_view
name 'blablabla' is not defined
Request Method:     GET
Request URL:        http://localhost:8000/my_view
Exception Type:     NameError
Exception Value:    name 'blablabla' is not defined
Exception Location: /usr/lib/python2.4/site-packages/Django-0.95-py2.4.egg/django/template/__init__.py in get_library, line 880

I'm not certain that there's a way around this (I'm relatively new to Python), but I suspect it is possible to resolve by (somehow) determining which file/line the ImportError originated from/on, and adjusting the error message that is raised in the get_library() function. I'd be willing to spend time on a patch if I you're all willing to accept one. :)

Attachments (2)

patch_3349.diff (10.4 KB ) - added by durdinator 17 years ago.
Updated patch against r7574 of trunk
3349_r11897.diff (9.8 KB ) - added by Andrew Badr 15 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 by Chris Beaven, 18 years ago

Triage Stage: UnreviewedAccepted
Version: 0.95SVN

Thanks for the ticket, Chris. We're always willing to accept patches :)

It is a bit of an ugly error message - if you can find an elegant way around it then that'd be great.

comment:2 by Jens Diemer, 18 years ago

I have a similar case. The error message is misleading:

InvalidTemplateLibrary: Could not load template library from django.templatetags.pylucid_tags, No module named template

The path django.templatetags.pylucid_tags is wrong. My Module is in PyLucid.templatetags.pylucid_tags.
So i have thought my Lib was not found and therefore was not loaded.

I ignored the last part of the message: No module named template. That is the actual error.

I don't know how to patch this...

comment:3 by Chris Wagner, 18 years ago

Jens - Do you know how to work around this problem? It doesn't actually prevent you from using template tags; it's just a really confusing error message. The problem is likely that you are trying to import a non-existent module from within your template tag library. This was what caused the error message for me. (I think that I may have even copied the culprit code from a tutorial somewhere, which didn't help me in tracking down the cause.)

in reply to:  3 comment:4 by javinievas@…, 18 years ago

Replying to Chris Wagner:

Jens - Do you know how to work around this problem? It doesn't actually prevent you from using template tags; it's just a really confusing error message. The problem is likely that you are trying to import a non-existent module from within your template tag library. This was what caused the error message for me. (I think that I may have even copied the culprit code from a tutorial somewhere, which didn't help me in tracking down the cause.)

After a few debugging and reversed engineering... I've found that django.templatetags.something will search something across the templatetags folder of your apps. So this is not the problem. I haven't found yet why it is happening, because I'm getting the same error and I don't have any wrong import.

comment:5 by James Bennett, 18 years ago

This discussion on django-develoeprs explains a little bit of how Django loads template tag libraries, and why the error message says it's looking in django.templatetags: http://groups.google.com/group/django-developers/browse_frm/thread/f9aec919b1449539/

comment:6 by durdinator, 17 years ago

Owner: changed from nobody to durdinator

comment:7 by durdinator, 17 years ago

Has patch: set

This patch is a little wider in scope than the ticket. I took the core problem here and ran with it -- that an ImportError can have various causes, and raising a different exception instead makes the traceback useless for debugging.

So this patch, in numerous places, still raises the TemplateSyntaxError or other appropriate exception, but maintains the traceback history down to whatever caused the original exception.

This is done with a helper function, django.core.exceptions.wrap_and_raise()

comment:8 by James Bennett, 17 years ago

#2298 was a duplicate.

comment:9 by durdinator, 17 years ago

Cc: me@… added

by durdinator, 17 years ago

Attachment: patch_3349.diff added

Updated patch against r7574 of trunk

comment:10 by durdinator, 17 years ago

Triage Stage: AcceptedReady for checkin

comment:11 by Marc Fargas, 17 years ago

Summary: If an ImportError occurs within a custom template tag library, a rather confusing error message is producedIf an ImportError occurs within some loaders a rather confusing exception is raised.

Given that the patch not only affects templatetags I'm changing the summary line accordingly.

comment:12 by Russell Keith-Magee, 17 years ago

Needs tests: set
Triage Stage: Ready for checkinAccepted

Not ready for checkin - needs tests.

comment:13 by mrts, 16 years ago

milestone: 1.2

Re-targeting for 1.2.

comment:14 by ramusus, 15 years ago

Cc: ramusus@… added

comment:15 by Andrew Badr, 15 years ago

Cc: andrewbadr.etc@… added
Owner: changed from durdinator to Andrew Badr

I can update this and write tests for 1.2

by Andrew Badr, 15 years ago

Attachment: 3349_r11897.diff added

comment:17 by Andrew Badr, 15 years ago

Here's an updated version of the patch, which passes all the existing tests. Still not sure how to write a good test for this. Anyone?

comment:18 by Andrew Badr, 15 years ago

Owner: changed from Andrew Badr to nobody

comment:19 by James Bennett, 15 years ago

milestone: 1.21.3

Given that this is really more of a minor annoyance than a bug, and that we've survived with it thus far, I think it's safe to say it's non-critical for 1.2 and can be handled on the 1.3 cycle.

comment:20 by Ramiro Morales, 15 years ago

Resolution: fixed
Status: newclosed

This has been fixed in r12944. I now get:

ImportError at /view/

No module named notthere

Request Method: 	GET
Request URL: 	http://localhost:8000/view/
Django Version: 	1.2 beta 1
Exception Type: 	ImportError
Exception Value: 	

No module named notthere

Exception Location: 	/home/r/r/dtest07/t3349/templatetags/tag_lib.py in <module>, line 1

This ticket is three years old so IMHO it is safe to declare that the first release containing a fix for this is 1.2 without further backporting work needed.

comment:21 by Jacob, 13 years ago

milestone: 1.3

Milestone 1.3 deleted

Note: See TracTickets for help on using tickets.
Back to Top