title: Importing a Python Module inside Custom Django Templatetags
body: <p>I'm using <code>virtualenv</code> with my Python Django installation.</p>
  
  <p>Here is my <strong>directory structure</strong>:</p>
  
  <pre><code>project/
      dev_environ/
          lib/
              python2.6/
                  site-packages/
                      ...
                      django/
                      titlecase/   # &lt;-- The titlecase module
                      PIL/
                      ...
          bin/
              ...
              python  # &lt;-- Python
              ...
          include/
  
      django_project/
          localsite/
              templatetags/
                  __init__.py
                  smarttitle.py    # &lt;-- My templatetag module
          foo_app/
          bar_app/
          settings.py
          manage.py
  </code></pre>
  
  <p>If I start my Django shell and attempt to import <code>titlecase</code> everything is fine, because <code>titlecase</code> is in the <code>sys.path</code> at <code>dev_environ/lib/python2.6/site-packages/titlecase</code>.</p>
  
  <pre><code>$:django_project cwilcox$ ../dev_environ/bin/python manage.py shell
  Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
  [GCC 4.2.1 (Apple Inc. build 5646)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  (InteractiveConsole)
  &gt;&gt;&gt; import titlecase  # &lt;-- No Import Error!
  &gt;&gt;&gt; 
  </code></pre>
  
  <p>I can even do an <code>import titlecase</code> inside my <code>settings.py</code> file without error.</p>
  
  <p>However when I try to <code>import titlecase</code> in my templatetag library <code>smarttitle.py</code> I get an <code>ImportError</code>. </p>
  
  <p><strong>smarttitle.py</strong> is as follows.</p>
  
  <pre><code>from django import template
  from django.template.defaultfilters import stringfilter
  register = template.Library()
  from titlecase import titlecase as _to_titlecase
  
  @register.filter
  @stringfilter
  def smarttitle(value):
      return _to_titlecase(value)
  smarttitle.is_safe = True
  </code></pre>
  
  <p>Not only that but I can even <code>import titlecase</code> inside the view that renders the template that tries to <code>{% load smarttitle %}</code> and there's no error.</p>
  
  <p>My Django dev server is started with...</p>
  
  <pre><code>../dev_environ/bin/python manage.py runserver
  </code></pre>
  
  <p><em>In Summary:</em></p>
  
  <p>I can import the <code>titlecase</code> module <em>anywhere</em> except inside this templatetag library, where it throws an <code>ImportError</code>! What gives?! Any ideas?</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: I tried first running <code>source dev_environ/bin/activate</code> to switch my shell env to my virtualenv, but that didn't help--I'm still getting the ImportError inside my templatetag module. I was already calling the proper python binary manually.</p>