Reputation: 569
I thought the correct order of imports in Python was the one described by the first answer of the question: What's the correct way to sort Python `import x` and `from x import y` statements?
Therefore, this code should be correct:
import os
import time
import yaml
from collections import OrderedDict
from xtesting.core import testcase
However, when I run Pylint I get:
C: 5, 0: standard import "from collections import OrderedDict" should be placed before "import yaml" (wrong-import-order)
So I guess "yaml" is not a standard library. Should then the correct way to do it be this one (even if it is uglier and less readable)?
import os
import time
from collections import OrderedDict
import yaml
from xtesting.core import testcase
Upvotes: 1
Views: 1995
Reputation: 76578
PyYAML is not part of the standard Python library and imports from the standard library, whether generic (import os
) or specific (from collections import OrderedDict
) should come first.
You should, IMO, lexicographically sort on the module names in the sections and separate the sections with an empty line:
from collections import OrderedDict
import os
import time
from xtesting.core import testcase
import yaml
There are some that want the generic ones to all come first, in each section:
import os
import time
from collections import OrderedDict
import yaml
from xtesting.core import testcase
This looks nicer, but it makes it easier to overlook specific imports after a long generic lists. And it also separates a generic and specific import from one and the same module, which, IMO, is bad:
import yaml
from xtesting.core import testcase
from yaml import safe_load
Upvotes: 2