Opened 3 years ago

Closed 18 months ago

Last modified 18 months ago

#24646 closed Bug (fixed)

File names jinja2.py cannot import jinja2

Reported by: Daniel Greenfeld Owned by: nobody
Component: Documentation Version: 1.8
Severity: Normal Keywords: jinja2
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

On https://docs.djangoproject.com/en/1.8/topics/templates/ it says to create a file at myproject/jinja2.py which includes import jinja2 , which breaks on the fact that you can't import a module into something named the same as the module.

Change History (13)

comment:1 Changed 3 years ago by Nick Pope

I presume that you are using Python 2? Try:

from __future__ import absolute_import
import jinja2
# ...

This should probably be documented...

comment:2 Changed 3 years ago by Daniel Greenfeld

Probably. ;)

My entire history of documentation contributions to Django can be summed up as, "If readers copy/paste the code examples and they don't work, then the documentation is broken."

Last edited 3 years ago by Daniel Greenfeld (previous) (diff)

comment:3 Changed 3 years ago by Aymeric Augustin

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:5 Changed 3 years ago by Tim Graham

Has patch: set

Good?

diff --git a/docs/topics/templates.txt b/docs/topics/templates.txt
index 9f0048b..d3b951a 100644
--- a/docs/topics/templates.txt
+++ b/docs/topics/templates.txt
@@ -415,6 +415,8 @@ APIs, you must configure them into the environment.
 
 For example, you can create ``myproject/jinja2.py`` with this content::
 
+    from __future__ import absolute_import  # Python 2 only
+
     from django.contrib.staticfiles.storage import staticfiles_storage
     from django.core.urlresolvers import reverse
 

comment:6 Changed 3 years ago by Simon Charette

Triage Stage: AcceptedReady for checkin

comment:7 Changed 3 years ago by Aymeric Augustin

Yes, you can ship that. Thanks!

comment:8 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In d974c89:

Fixed #24646 -- Fixed jinja2 example on Python 2.

comment:9 Changed 3 years ago by Tim Graham <timograham@…>

In 9e5e491:

[1.8.x] Fixed #24646 -- Fixed jinja2 example on Python 2.

Backport of d974c89ebb8a59d91f664035d98d42dbb5cdd592 from master

comment:10 Changed 18 months ago by taigit

Resolution: fixed
Status: closednew

The previous fix still has the same problem and causes a django error.
see: http://stackoverflow.com/questions/40873096/error-when-using-jinja2-as-django-template-engine-cannot-import-name-environme/40873097#40873097

A fix that will get rid of the problem:
change: myproject/jinja2.py
to myproject/jinja2_bridge.py

in the documentation.

comment:11 Changed 18 months ago by taigit

Easy pickings: set

comment:12 Changed 18 months ago by Tim Graham

Resolution: fixed
Status: newclosed

I can't reproduce a problem unless I remove the from __future__ import absolute_import that the commit for this ticket added. Could you please provide a sample project that reproduces the problem? Ideally, you would use our support channnels rather than this ticket tracker. Thanks.

comment:13 Changed 18 months ago by Aymeric Augustin

The point is almost moot as the next version of Django will drop support for Python 2, and this problem can't happen on Python 3.

Note: See TracTickets for help on using tickets.
Back to Top