Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Py3k: What's more pythonic - one import with commas or many imports?

What is more pythonic ?

import os
import sys
import getopt
...

or

import os,sys,getopt,...

?

like image 796
Grzegorz Wierzowiecki Avatar asked Jan 12 '12 17:01

Grzegorz Wierzowiecki


People also ask

Does the order of Python imports matter?

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.

How do you arrange imports in Python?

Organize imports into groups: first standard library imports, then third-party imports, and finally local application or library imports. Order imports alphabetically within each group. Prefer absolute imports over relative imports. Avoid wildcard imports like from module import * .

Can you import more than one module in Python?

You can write multiple modules separated by commas after the import statement, but this is not recommended in PEP8. Imports should usually be on separate lines. If you use from to import functions, variables, classes, etc., as explained next, you can separate them with a comma.


1 Answers

From PEP 8:

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
like image 195
Sven Marnach Avatar answered Sep 30 '22 19:09

Sven Marnach