3

The PEP8 style guide section on imports seems to be a bit ambiguous.

From: https://www.python.org/dev/peps/pep-0008/#imports

The first part makes sense:

# Imports should usually be on separate lines:

# Correct:
import os
import sys

# Wrong:
import sys, os

But then it goes on to say:

# It's okay to say this though:

# Correct:
from subprocess import Popen, PIPE

How should we interpret this? subprocess is a module, so is PEP8 saying it's just OK to import multiple things from a single module on one line? Or is it saying it's OK to import any number of things from a higher level entity on one line? I.e. is importing multiple modules from a package good style?

I'm sure arguments could be made for either style, so I'm not asking for opinions on that, but is there an authoritative source on what the PEP8 intent is here?

Rich Smith
  • 1,675
  • 12
  • 24
  • 1
    As far as I know, you use different import statements for importing different packages/modules/libraries (however you want to call them), and use a single import statement when importing multiple *objects* (functions, classes, values) from the same package. – jfaccioni May 29 '20 at 13:42
  • 1
    I would expect the former: one *module* per `import` statement, as names defined by a module are distinct from modules belonging to a package. (Something like `import package.module1` does not imply that `module1` is an attribute of `package`; if it is, it's because `package` explicitly defines it.) – chepner May 29 '20 at 13:43
  • I've always interpreted it that way too, but it also seems to be commonly interpreted the second way. See for example the highly-upvoted answer to this question: https://stackoverflow.com/questions/15011367/python-module-import-single-line-vs-multi-line – Rich Smith May 29 '20 at 13:46

0 Answers0