Jinja2 Environment ImportError in Django 1.10 with Python 3.5
<p>I have a Django 1.10 project (with Python 3.5, and Jinja2 2.8 for templates) setup like this:</p>

<p><a href="http://i.stack.imgur.com/QSdtj.png" rel="nofollow"><img src="http://i.stack.imgur.com/QSdtj.png" alt="Directory structure"></a></p>

<p>*Consider the red marks as <code>mysite</code></p>

<p>File <code>jinja2.py</code> defines this:</p>

<pre><code>from __future__ import absolute_import

from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse

from jinja2 import Environment


def environment(**options):
    env = Environment(**options)
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': reverse,
    })
    return env
</code></pre>

<p>And this file is called in <code>settings.py</code> like this:</p>

<pre><code>{
    'BACKEND': 'django.template.backends.jinja2.Jinja2',
    'DIRS': [],
    'APP_DIRS': True,
    'OPTIONS': {
        'environment': 'mysite.jinja2.environment',
    },
},
</code></pre>

<p>But when I run the server and visit a URL with view that calls a Jinja2 template, I get the following error:</p>

<p><a href="http://i.stack.imgur.com/WksbB.png" rel="nofollow"><img src="http://i.stack.imgur.com/WksbB.png" alt="ImportError"></a></p>

<p>I spent several hours searching the Internet to find a solution, but unable to resolve this.</p>

<p>The entire setup was done following the official Django documentation: <a href="https://docs.djangoproject.com/en/1.10/topics/templates/#django.template.backends.jinja2.Jinja2" rel="nofollow">https://docs.djangoproject.com/en/1.10/topics/templates/#django.template.backends.jinja2.Jinja2</a></p>

<p>Thanks for your response(s)</p>

<p>I think you put the <code>jinja2.py</code> in a wrong position. It should be in the dir <code>mysite/</code>.</p>

<p>This is the directory structure of my django project <code>mysite</code>(Jin2 is a app dir), which can run normally:</p>

<pre><code>â /tmp/jinja/mysite $ tree . -I '*.pyc'
.
âââ db.sqlite3
âââ jin2
âÂ Â  âââ admin.py
âÂ Â  âââ apps.py
âÂ Â  âââ __init__.py
âÂ Â  âââ jinja2
âÂ Â  âÂ Â  âââ index.html
âÂ Â  âââ migrations
âÂ Â  âÂ Â  âââ __init__.py
âÂ Â  âÂ Â  âââ __pycache__
âÂ Â  âââ models.py
âÂ Â  âââ __pycache__
âÂ Â  âââ tests.py
âÂ Â  âââ urls.py
âÂ Â  âââ views.py
âââ manage.py
âââ mysite
âÂ Â  âââ __init__.py
âÂ Â  âââ jinja2.py
âÂ Â  âââ __pycache__
âÂ Â  âââ settings.py
âÂ Â  âââ urls.py
âÂ Â  âââ wsgi.py
âââ __pycache__
</code></pre>

<p>My <code>settings.py</code> and <code>jinja2.py</code> are same as yours. The  only difference is that I put the <code>jinja2.py</code> in the mysite dir.</p>

<p>And you should delete the <code>from __future__ import absolute_import</code> in the <code>jinja2.py</code>, it is only needed in python2.</p>

python django