Code

Opened 7 years ago

Closed 4 years ago

Last modified 3 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: master
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: UI/UX:

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 6 years ago.
Updated patch against r7574 of trunk
3349_r11897.diff (9.8 KB) - added by andrewbadr 5 years ago.

Download all attachments as: .zip

Change History (23)

comment:1 Changed 7 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Version changed from 0.95 to SVN

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 Changed 7 years ago by Jens Diemer

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 follow-up: Changed 7 years ago by 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.)

comment:4 in reply to: ↑ 3 Changed 7 years ago by javinievas@…

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 Changed 7 years ago by ubernostrum

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 Changed 7 years ago by durdinator

  • Owner changed from nobody to durdinator

comment:7 Changed 7 years ago by durdinator

  • 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 Changed 7 years ago by ubernostrum

#2298 was a duplicate.

comment:9 Changed 7 years ago by durdinator

  • Cc me@… added

Changed 6 years ago by durdinator

Updated patch against r7574 of trunk

comment:10 Changed 6 years ago by durdinator

  • Triage Stage changed from Accepted to Ready for checkin

comment:11 Changed 6 years ago by telenieko

  • Summary changed from If an ImportError occurs within a custom template tag library, a rather confusing error message is produced to If 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 Changed 6 years ago by russellm

  • Needs tests set
  • Triage Stage changed from Ready for checkin to Accepted

Not ready for checkin - needs tests.

comment:13 Changed 5 years ago by mrts

  • milestone set to 1.2

Re-targeting for 1.2.

comment:14 Changed 5 years ago by ramusus

  • Cc ramusus@… added

comment:15 Changed 5 years ago by andrewbadr

  • Cc andrewbadr.etc@… added
  • Owner changed from durdinator to andrewbadr

I can update this and write tests for 1.2

comment:16 Changed 5 years ago by andrewbadr

Changed 5 years ago by andrewbadr

comment:17 Changed 5 years ago by andrewbadr

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 Changed 4 years ago by andrewbadr

  • Owner changed from andrewbadr to nobody

comment:19 Changed 4 years ago by ubernostrum

  • milestone changed from 1.2 to 1.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 Changed 4 years ago by ramiro

  • Resolution set to fixed
  • Status changed from new to closed

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 Changed 3 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.