Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there anything bad about having multiple imports on one line?

Tags:

python

import

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?

like image 787
Vladimir Putin Avatar asked Jun 17 '14 19:06

Vladimir Putin


People also ask

Is using import * Bad?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.

Should imports always be at top?

PEP 8 recommends putting imports at the top. It's often more convenient to get ImportError s when you first run your program rather than when your program first calls your function. Putting imports in the function scope can help avoid issues with circular imports.

Can two modules import each other?

Modules can import other modules. It is customary but not required to place all import statements at the beginning of a module (or script, for that matter). The imported module names, if placed at the top level of a module (outside any functions or classes), are added to the module's global namespace.


Video Answer


3 Answers

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.

like image 172
pcurry Avatar answered Oct 19 '22 19:10

pcurry


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.

like image 20
karthikr Avatar answered Oct 19 '22 20:10

karthikr


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.

like image 12
Yeo Avatar answered Oct 19 '22 19:10

Yeo