24

When I'm programming in Python and I need to import multiple modules, I usually do I like this:

import random, time, matplotlib, cheese, doge

Then when I read over other people's code, this is what I see:

import random
import time
import matplotlib
import cheese
import doge

Why is this? Is there any difference between the two styles?

2
  • I answered with a lot of benefits of one import per line - most of which are solving a problem in maintaining code with multiple imports per line, particularly in a large Python codebase.
    – pcurry
    Jul 30, 2014 at 1:07
  • Can this be done in java as well ? May 27, 2022 at 4:32

5 Answers 5

30

The practice of one import per line is standardized in PEP8, and following a common standard is reason enough to do as others do. Following a common standard follows the Principle of Least Astonishment, making it easier for people familiar with the standard to read and modify your code.

Even if you don't care about PEP8, though, one import per line makes your code more maintainable.

  • Imports are easier to skim/read:

    • It's easier to see that you are getting a fred in import fred than in import barney, betty, wilma, fred, bambam, pebbles
  • Imports are easier to locate:

    • Searching for "import fred" will find import fred and import fred, wilma, pebbles, but will not find import barney, fred
  • Imports are easier to edit:

    • Inserting and removing an entire line is fast in most editors.
    • There is only one module per line, so you don't have to search in the line to find the thing you wish to edit - it's at the end.
    • Relocating an import inside a module is just moving a whole line.
    • Copying one of several imports to another Python module is a copy-paste of a line, rather than that copy-paste followed by trimming off the other imports you don't want.
  • Imports are easier to maintain:

    • Each changed module has its own line in the change-set - you don't have to read a line to figure out which module or modules changed.
    • Missing and added modules effect the line count on the file and in the change-set.
    • Typos are easier to pick out and correct on visual skim of the change-set.

One import per line would be a good idea even if it weren't the standard. Since it is the standard, it's doubly the best way to go.

2
  • 5
    I love the intuition rather than mere reference to PEP8!
    – 3pitt
    Mar 5, 2018 at 15:54
  • 1
    I wish there was at least some rationale in PEP8. Your reasons are good (though I think avoiding merge conflicts is at least as important). The downside is that if you do a good job of scoping your imports you end up with a big fraction of the total code in imports. Two imports as shown in PEP8 is not bad, but sometimes there are 10. Jun 24, 2020 at 23:20
17

As per PEP-8 (The Style Guide for Python Code)

Imports should usually be on separate lines, for e.g

Yes: import os
     import sys

No:  import sys, os 

It's okay to say this though:

from subprocess import Popen, PIPE

To answer your question - both would work fine, but one is not conformant with the PEP8 guidelines.

5
  • 2
    I usually prefer to keep all my imports on one line, unless the line char count exceeds 80, just to keep my code shorter. Just in case I have 50 imports for some reason. Jun 17, 2014 at 19:43
  • Correct - I see quite a few programmers do that. The guidelines are a way to bring consistency across the board for developers. They are more of conventions - than "errors" so to speak. In my experience/opinion, it is a good idea to follow the guidelines.
    – karthikr
    Jun 17, 2014 at 19:45
  • 1
    I also like the extensions of docs.openstack.org/developer/hacking about imports. Jun 17, 2014 at 20:06
  • 1
    @holdenweb so you don't have to scroll for ages to see the meat of your code.
    – ELI7VH
    Sep 15, 2018 at 17:46
  • So presumably you don't add vertical whitespace after functions, classes and methods because you don't want to scroll? PEP8 suggests certain rules and is intended to guide authors, but it's not a cast-iron rule. If you want to do something else that's perfectly OK. I try not to be too picky. The first thing code needs to do is work! The Principle of Least Astonishment just means code that follows PEP8 is likely going to be easier for others to read.
    – holdenweb
    Sep 15, 2018 at 22:10
12

I don't like to follow blindly without valid reason. As PEP20: Zen of Python states that "Readability Counts"

PEP8 "single line per import" works for general perspective. Although I respect his (i.e. Guido) opinion, I wouldn't always strictly follow this conventions all the time.

The exception for this rule is only when the # of code is smaller than the # of module import. e.g. 2 lines of code, but 4 module import.

This is more readable: (in my opinion)

import os, sys, math, time

def add_special():
    return time.time() + math.floor(math.pow(sys.api_version + os.getpid(), 2))

instead of this

import os
import sys
import math
import time

def add_special():
    return time.time() + math.floor(math.pow(sys.api_version + os.getpid(), 2))

But this readability matter differs for each individuals.

1
  • I sort of agree. I also think that one-liner is really dense, and maybe should be broken out. The main point for me is that I'm almost always working on maintaining code, and simple imports are easier to maintain, even if they are verbose.
    – pcurry
    Dec 3, 2020 at 16:43
0

PEP-8, the official Python style guide, mandates that one package or module should be imported per line.

It is considered good style, and generally standardization makes programs easy to read. I don't think there are substantial differences under the hood to worry about, if that's what you're asking.

0

Those two examples are functionally equivalent. However, PEP 8, the official style-guide for Python, has a section here that condemns the practice of placing multiple imports on one line:

Imports should usually be on separate lines, e.g.:

    Yes: import os
         import sys

    No:  import sys, os

It's okay to say this though:

    from subprocess import Popen, PIPE

Thus, many Python programmers place only one import per line in order to follow this guideline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.