Is there a recommended format for multi-line imports? Is there a recommended format for multi-line imports? python python

Is there a recommended format for multi-line imports?


Personally I go with parentheses when importing more than one component and sort them alphabetically. Like so:

from Tkinter import (    Button,    Canvas,    DISABLED,    END,    Entry,    Frame,    LEFT,    NORMAL,    RIDGE,    Text,    Tk,)

This has the added advantage of easily seeing what components have been added / removed in each commit or PR.

Overall though it's a personal preference and I would advise you to go with whatever looks best to you.


Your examples seem to stem from PEP 328. There, the parenthesis-notation is proposed for exactly this problem, so probably I'd choose this one.


I would go with the parenthesis notation from the PEP328 with newlines added before and after parentheses:

from Tkinter import (    Tk, Frame, Button, Entry, Canvas, Text,     LEFT, DISABLED, NORMAL, RIDGE, END)

This is the format which Django uses:

from django.test.client import Client, RequestFactoryfrom django.test.testcases import (    LiveServerTestCase, SimpleTestCase, TestCase, TransactionTestCase,    skipIfDBFeature, skipUnlessAnyDBFeature, skipUnlessDBFeature,)from django.test.utils import (    ignore_warnings, modify_settings, override_settings,    override_system_checks, tag,)