Jinja2 with Django 1.8c1 & cannot import name 'Environment'

1,996 views
Skip to first unread message

Rolf Brudeseth

unread,
Mar 28, 2015, 6:00:47 PM3/28/15
to django...@googlegroups.com
Does anybody see what I may be doing wrong here?

I followed this example:
https://github.com/aaugustin/mtefd/blob/master/multiple-template-engines.rst#jinja2-backend


The project is at:
/www/pbs_site

/www/pbs_site$ python manage.py check
System check identified no issues (0 silenced).

##### Error #####

ImportError at /

cannot import name 'Environment'

Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8c1
Exception Type: ImportError
Exception Value:

cannot import name 'Environment'

Exception Location: /www/pbs_site/jinja2.py in <module>, line 4
Python Executable: /www/pbs-venv/bin/python
Python Version: 3.4.3
Python Path:

['/www/pbs_site',
'/www/pbs_site/pbs_site',
'/www/pbs-venv/bin',
'/usr/local/lib/python34.zip',
'/www/Python-3.4.3/Lib',
'/www/Python-3.4.3/Lib/plat-linux',
'/www/Python-3.4.3/build/lib.linux-i686-3.4',
'/www/pbs-venv/lib/python3.4/site-packages']

Server time: Fri, 27 Mar 2015 11:19:56 -0500

##### Code #####

/www/pbs_site$ grep ^TEMPLATES -A 23 pbs_site/settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'NAME': 'jinja2',
'DIRS': [],
'APP_DIRS': False,
'OPTIONS': {
'environment': 'jinja2.Environment',
},
},
]

/www/pbs_site$ cat jinja2.py
from django.contrib.staticfiles.storage import staticfiles_storage
from django.core.urlresolvers import reverse

from jinja2 import Environment


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

--
Rolf

Simon Charette

unread,
Mar 28, 2015, 7:16:41 PM3/28/15
to django...@googlegroups.com
Hi Rolf,

Since your `jinja2.py` file sits before your installed `jinja2` package directory in your PYTHONPATH you end up trying to import `Environement` from your the module you're defining and not the installed `jinja2` module.

Rename your jinja2.py file something else and things should be working.

Simon

Rolf Brudeseth

unread,
Mar 29, 2015, 2:53:49 PM3/29/15
to django...@googlegroups.com

Rename your jinja2.py file something else and things should be working.

 
I am having some problems wrapping my head around how to implement this.

When I rename the file I created to something arbitrary I get:

Exception Value:
No module named 'jinja2'
Exception Location: /www/pbs-venv/lib/python3.4/site-packages/django/template/backends/jinja2.py in <module>, line 6

Not sure why the file above has this statement on line 6 if it is not to reference the file I created:
import jinja2

Furthermore, this file does not have an 'Environment' class so I am not sure how the following statement will work in the file I am supposed to create:
from jinja2 import Environment

Finally, I am not sure how to reference the file I need to create. Is that specified in settings.py?

Thanks
 

Simon Charette

unread,
Mar 29, 2015, 4:19:52 PM3/29/15
to django...@googlegroups.com
Hi Rolf,

Did you install jinja2 in your python environment (I assumed /www/pbs-venv was a virtualenv)?

What does /www/pbs-venv/bin/pip --freeze outputs?

Simon

Rolf Brudeseth

unread,
Mar 29, 2015, 4:40:33 PM3/29/15
to django...@googlegroups.com
Hello Simon,
 
Did you install jinja2 in your python environment (I assumed /www/pbs-venv was a virtualenv)?

No, Jinja2 is integrated into Django 1.8c1.

https://docs.djangoproject.com/en/dev/releases/1.8/

- I am using pyvenv which is integrated into Python, not the virtualenv pypi package.
pbs-venv was created this way:
./Python-3.4.3/python ./Python-3.4.3/Tools/scripts/pyvenv pbs-venv
 

What does /www/pbs-venv/bin/pip --freeze outputs?

www$ pip --freeze
Usage:  
  pip <command> [options]
no such option: --freeze

Thanks

Simon Charette

unread,
Mar 29, 2015, 4:55:59 PM3/29/15
to django...@googlegroups.com
Hi Rolf,

Django 1.8+ adds built-in support but doesn't ship with Jinja2, the same way it has built-in support for PostgreSQL but doesn't ship the psycopg2 package.

You must install it in your environment just like you did with the Django package.

Simon

Rolf Brudeseth

unread,
Mar 30, 2015, 7:47:46 PM3/30/15
to django...@googlegroups.com


Django 1.8+ adds built-in support but doesn't ship with Jinja2, the same way it has built-in support for PostgreSQL but doesn't ship the psycopg2 package.

You must install it in your environment just like you did with the Django package.


Thanks Simon. Installing Jinja2 fixed my issue.

Rolf

Simon Charette

unread,
Mar 30, 2015, 11:13:48 PM3/30/15
to django...@googlegroups.com
You're welcome Rolf.

Your question lead an initiative by Tim to clarify Jinja2 needs to be installed.

Cheers,
Simon
Reply all
Reply to author
Forward
0 new messages