Import a Python module into a Jinja template?

Within the template, no, you cannot import python code.

The way to do this is to register the function as a jinja2 custom filter, like this:

In your python file:

from dates.format import timesince

environment = jinja2.Environment(whatever)
environment.filters['timesince'] = timesince
# render template here

In your template:

{% macro time(mytime) %}
<a title="{{ mytime }}">{{ mytime|timesince }}</a>
{% endmacro %}

Just pass the function into the template, like so

from dates.format import timesince
your_template.render(timesince)

and in the template, just call it like any other function,

{% macro time(mytime) %}
    <a title="{{ mytime }}">{{ timesince(mytime) }}</a>
{% endmacro %}

Functions are first-class citizens in python, so you can pass them around just like anything else. You could even pass in a whole module if you wanted.