views:

700

answers:

3

I've been pulling my hair out trying to solve this problem and I've tried everything and I have no ideas left.

I keep seeing this error: Exception Value: 'thumbnail' is not a valid tag library: Could not load template library from django.templatetags.thumbnail, No module named sorl.thumbnail.main

$DJANGO_PACKAGES/sorl/thumbnail/main.py DOES exist.

Here's what I did to setup,

  1. downloaded latest sorl-thumbnail and added its location to the python path in .bash_profile

  2. included 'sorl.thumbnail' in INSTALLED_APPS (in django's settings.py)

  3. used the {% load thumbnail %} tag in a django template

It would seem obvious sorl-thumbnail is not installed correctly, but I'm able to import thumbnail from the python shell and the django shell (when I use {% load thumbnail %} it brings this error). Also, there are no typos in related files (I've checked many many times).

+1  A: 

I would venture to guess this is a $PYTHONPATH issue. Is it possible that the "thumbnail" directory is on the path and not "sorl"? I suspect this is the issue because you do not want to be able to type "import thumbnail" on the Python interpreter. You should instead have to type "import sorl.thumbnail".

Another thing to check is to print the module name after importing:

>>> import thumbnail
>>> print thumbnail

This will display the filesystem location where the module was found, in case it's loading another copy from somewhere you do not expect.

You also want to make sure your current working directory is not the root ../sorl/ location (ie. don't run python from the sorl folder). This will allow you to import thumbnail straight-away.

You should check your full Python path (it will be more than $PYTHONPATH) from within the python interpreter to verify your package locations:

>>> import sys
>>> print sys.path

It might also be helpful to learn more about Python importing

jdl2003
I have the sorl directory and the thumbnail directory in $PYTHONPATH. This is how my $PYTHONPATH looks like (I seperated the paths for clarity):export PYTHONPATH=$PYTHONPATH:$HOME/django_src:$HOME/django_projects:$HOME/django_projects/site_project/sorl:$HOME/django_projects/site_project:$HOME/django_projects/site_project/sorl/thumbnail
vector
I am able to import _both_ sorl.thumbnail and thumbnail from the shell
vector
That is the problem. You do not want thumbnail to be on the PYTHONPATH, only the root __sorl__ directory.
jdl2003
jdl2003,OK. I'm trying to alter PYTHONPATH so that only sorl directory can be directly imported, however, after changing my .bash_profile, I'm still able import thumbnail from the python shell.This is in my .bash_profile:export PATH=$PATH:$HOME/django_src/django/binexport PYTHONPATH=$PYTHONPATH:$HOME/django_src:$HOME/django_projects:$HOME/django_projects/site_projectWhen I import 'thumbnail' and then print 'thumbnail' this is the dir path it shows:<module 'thumbnail' from '/home/user/django_projects/site_project/sorl/thumbnail/__init__.pyc'>
vector
also, .bash_profile was updated with 'source .bash_profile'
vector
Sorry my last post wasn't very clear. You seem to be headed in the right direction. Make sure your working directory is not ../sorl/ before you run python and check your imports. I added some additional tests above.
jdl2003
jdl2003, OK, thanks for the update above, I commented out the $PYTHONPATH statement in my .bash_profile and still when I printed out sys.path, many different paths came up, -I'll read through the material, remove the paths giving access to thumbnail, and I'll post an update here when that's done
vector
sys.path elements can be accessed individually, for example 'print sys.path[2]'. 'del sys.path[4]' removes the path to thumbnail so _only_ sorl.thumbnail will import. However, each time I start the python shell, the path that I deleted is returned, so... still looking into it...
vector
I altered the path to thumbnail, -the path was $HOME/django_projects/site_project/sorl/thumbnail and now its $HOME/django_projects/site_project/sorl/sorl/thumbnail
vector
here is my settings.py file, with paths to sorl commented out, maybe I'm not specifying the path correctly... http://pastebin.ca/1549982
vector
use this link instead: http://pastebin.ca/1549984
vector
I think that the settings.py file might be adding the thumbnail path to sys.path when INSTALLED_APPS has site_project.sorl.sorl.thumbnail in it. :(
vector
I'm back where I started. now, no matter what I add to my INSTALLED_APPS in settings.py, django yields a very ugly purple-colored TemplateSyntaxError page.I've tried adding 'sorl.thumbnail', 'site_project.sorl', 'site_project.sorl.sorl', and 'site_project.sorl.sorl.thumbnail'.All of those yield the error.
vector
**sigh** I'm not able to permanently remove the python sys.path item leading to thumbnail. The APP_INSTALL item in settings.py is not causing this problem and I'm not sure why this is happening.
vector
Is it possible that you use setup.py or another means of installing the thumbnail program at some point? You seem to have multiple things going on. My next suggestion would be to remove the sorl folder from your project folder into it's own location. I sometimes make a 'python-local' directory in my home folder and put that on my path (ie. $PYTHONPATH=/home/user/python-local). Then I copy any third party apps into that (so, /home/user/python-local/sorl). If all else fails, I would do this, delete all instances of sorl and start over by downloading it again and putting it in python-local.
jdl2003
A: 

Problem solved.

When following the django book, it is suggested to create apps within a project directory and to refer to these apps in the INSTALLED APPS statement with a path that begins from the directory containing the project, for example, 'siteproject.books'. I was not able to give django access to apps without appending that directory name to the file path, so, for example, I was not able to simply use 'books', but needed to use 'siteproject.books' in the INSTALLED APPS statement and this was the case with sorl.thumbnail, which needed to be referred to as siteproject.sorl.thumbnail. Other attempts to include 'sorl.thumbnail' would yield a very ugly un-formatted and confusing purple-colored error page (yes, the sorl directory was in $PYTHONPATH, so who knows why these attempts didn't work...).

Unfortunately, Django was yielding the 'undefined tag' error, which is a generalized error that it gives in many situations. It doesn't really mean anything and isn't useful for locating problems.

The problem was solved when I opened the files in the sorl directory and edited the python files. I found import statements that imported objects from the sorl directory and I appended the 'siteproject.*' to those paths and everything began to work.

vector
A: 

Here's another general tip on the unhelpful 'not a valid tag library' message: for tags that you create, it could be as simple as a syntax error.

Hat tip: 'Rock' on Django-users: http://groups.google.com/group/django-users/browse_thread/thread/d65db3940acf16c3?tvc=2

Scott Lawton