Explicit relative imports

278 views
Skip to first unread message

Aymeric Augustin

unread,
Nov 11, 2014, 4:51:19 PM11/11/14
to django-d...@googlegroups.com
Hello,

We’ve started using explicit relative imports in newer parts of the Django source tree. They’re short and readable. That’s good.

I would like to add guidelines about imports in the coding style guide in order to improve consistency.

My inclination would be to recommend relative imports within “components” but avoid them between “components”, where a component is:

- a well-defined sub-framework (django.core.cache, django.db, django.forms, django.template, etc.)
- a contrib app
- an app in the tests/ directory
- etc.

I would discourage going back into parent modules with relative imports because statements such as `from ...spam import eggs` are hard to parse.

You can see an example of this style in django.apps which has only three files.

What do you think?

--
Aymeric.



Marc Tamlyn

unread,
Nov 11, 2014, 6:01:55 PM11/11/14
to django-d...@googlegroups.com
I agree wholeheartedly. (Sorry, not much else to say...)


--
Aymeric.



--
You received this message because you are subscribed to the Google Groups "Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/9FFE2185-6F9D-41EE-8A19-16C5538E6B53%40polytechnique.org.
For more options, visit https://groups.google.com/d/optout.

Florian Apolloner

unread,
Nov 12, 2014, 5:31:32 AM11/12/14
to django-d...@googlegroups.com
Parent imports inside the sub-framework could make sense, otherwise the mixture of imports might look odd. As long as it stops at ".." and doesn't include more it's relatively fine imo.

Cheers,
Florian

Zach Borboa

unread,
Nov 12, 2014, 11:37:32 AM11/12/14
to django-d...@googlegroups.com
Aymeric, is this the example?

from .config import AppConfig # NOQA
from .registry import apps # NOQA

Aymeric Augustin

unread,
Nov 12, 2014, 12:30:45 PM11/12/14
to django-d...@googlegroups.com
Hi Zach,

django/apps/registry.py is a better example. It has relative imports from django/apps/config.py and absolute imports from other parts of Django.

-- 
Aymeric.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.

For more options, visit https://groups.google.com/d/optout.



--
Aymeric.

Jannis Leidel

unread,
Nov 12, 2014, 4:59:42 PM11/12/14
to django-d...@googlegroups.com
Yup, the way to go.

Jannis

Tom Christie

unread,
Nov 13, 2014, 8:57:15 AM11/13/14
to django-d...@googlegroups.com
Contrary voice here, but I don't dig explicit relative imports, it's just adds an extra decision point for no real benefit.

Personally I always recommend full absolute imports, ordered strictly alphabetically - there's then never any room for confusion or decision making around how the imports should be written, and it's always tidy and consistent.

Not looking to necessarily change the Django project's stance on that, but that's the style I use throughout my projects and it's trivial to stick to. 

Cheers,

  Tom

Marc Tamlyn

unread,
Nov 13, 2014, 9:50:56 AM11/13/14
to django-d...@googlegroups.com

Whilst I think it's a bit nicer to be explicit in smaller projects, there are a few places where django's structure can result in long import paths - e.g. django.contrib.gis.db.backends where I think relative imports would actually add clarity.

I tend to separate the relative section as the last block at the end of the imports which further show what is "within" a component, rather than it being buried within a large block.

In any case, explicit relative imports are greatly preferable to implicit ones.

Marc

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.

Tai Lee

unread,
Nov 13, 2014, 11:47:38 PM11/13/14
to django-d...@googlegroups.com
I also don't see much benefit in using relative imports. Code is read many more times than it is written, and I find having the full path makes it much easier to sort and visually scan imports, and to easily see at a glance exactly where to find what you need.

If they are to be used, I would recommend against any relative imports from parent packages. It should be limited to siblings only.

Cheers.
Tai.

tomv

unread,
Nov 15, 2014, 3:29:21 PM11/15/14
to django-d...@googlegroups.com
Tom, there's another way to remove confusion/decision making about import ordering: https://pypi.python.org/pypi/isort 

For Django it could be mentioned on the contrib docs and `isort --check-only` run with flake8.

Tim Graham

unread,
Nov 19, 2014, 4:01:45 PM11/19/14
to django-d...@googlegroups.com
I hope to get flake8 running on pull request builds soon. Then (unless there are objections) I will take a look at isort and see if it'll work for us in a similar fashion. From my perspective it would be great to get those checks automated so it's one less thing to think about during code reviews.
Reply all
Reply to author
Forward
0 new messages