Opened 10 years ago

Closed 8 years ago

Last modified 8 years 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 by Nick Pope, 10 years ago

I presume that you are using Python 2? Try:

from __future__ import absolute_import
import jinja2
# ...

This should probably be documented...

comment:2 by Daniel Greenfeld, 10 years ago

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 10 years ago by Daniel Greenfeld (previous) (diff)

comment:3 by Aymeric Augustin, 10 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:5 by Tim Graham, 10 years ago

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 by Simon Charette, 10 years ago

Triage Stage: AcceptedReady for checkin

comment:7 by Aymeric Augustin, 10 years ago

Yes, you can ship that. Thanks!

comment:8 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In d974c89:

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

comment:9 by Tim Graham <timograham@…>, 10 years ago

In 9e5e491:

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

Backport of d974c89ebb8a59d91f664035d98d42dbb5cdd592 from master

comment:10 by taigit, 8 years ago

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 by taigit, 8 years ago

Easy pickings: set

comment:12 by Tim Graham, 8 years ago

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 by Aymeric Augustin, 8 years ago

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