Vladimir Putin Vladimir Putin - 5 months ago 30
Python Question

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

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?

Answer

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.

Comments