2

I do not understand what is written in Google Python Style Guide about multiple imports per line.

Is it ok (according to Google Style Guide) to have it this way:

from wagtail.wagtailimages.blocks import ImageChooserBlock, EmbedBlock

or do I have to write it like this:

from wagtail.wagtailimages.blocks import ImageChooserBlock
from wagtail.wagtailembeds.blocks import EmbedBlock

Thanks.

  • Why do you need to be a slave to Google for everything? Import it as you like. – cs95 Jun 18 '17 at 10:21
  • Are you asking "Is it OK" according to the Google Python Style Guide, according to Google as a whole, or according to other programmers? My answer focusses on the first two while the other answers so far focus only on the last. Clarification from you would be appreciated. – Rory Daulton Jun 18 '17 at 11:13
  • @RoryDaulton Edited. Thanks. – Jiří Nádvorník Jun 18 '17 at 11:24

3 Answers3

1

Definitely the first way is fine. No one does the second option, that would be incredibly wasteful. You shouldn't import multiple different modules on the same line, but your first example is about getting multiple attributes from a single module.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
1

Use the first one.

from wagtail.wagtailimages.blocks import ImageChooserBlock, EmbedBlock

To import multiple members you can use parenthesis and add a few newlines. Here is an example:

from OpenGL.GLUT import (GLUT_DEPTH, GLUT_DOUBLE, GLUT_RGB, glutCreateWindow,
                         glutDisplayFunc, glutInit, glutInitDisplayMode,
                         glutInitWindowSize, glutMainLoop, glutSwapBuffers)

Please note the difference:

You should not import multiple modules in one line:

import os, sys, platform    # DO NOT DO THIS!

But importing multiple members is just fine:

from math import sin, cos

hint: check out isort

Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
0

If, in that linked style guide, you click on the right-facing triangle just under the section "Imports formatting", you get some positive and negative examples. This is one of the positive examples:

import foo
from foo import bar
from foo.bar import baz
from foo.bar import Quux
from Foob import at

As you can see, two items are imported from the single module foo.bar and they are listed on separate lines.

So in your two examples, the Google Style Guide wants you to use the second--separate lines. Note that I am not saying that is what you really should do, just that apparently the Google Style Guide says you should do it, which seems to be your question.

On the other hand, the Python Style Guidelines for The Chromium Projects, which is obviously also by Google, says

  • It is OK to import packages, modules, and things within a module.  This is mentioned solely because it contradicts the section on imports in the Google Style Guide (which, remember, is not an authority for Chromium OS).
    • Said another way, this is completely OK: from subprocess import Popen, PIPE

That example, stated to be OK, does import multiple items from one module in one line. So make your choice what your authority will be.

Rory Daulton
  • 21,934
  • 6
  • 42
  • 50