10

I have started learning python recently and ran pylint on my python file. I got following comments.

from os import listdir
from os.path import isfile, join

I the above two lines, the Pylinter comment is

C:  5, 0: Imports from package os are not grouped (ungrouped-imports)

How can I achieve that ?

And another comment is in below line

import mimetypes, time, csv, platform, base64, django, sys, os, math, uuid, linecache, logging, requests

C:  5, 0: standard import "import mimetypes, time, csv, platform, base64, django, sys, os, math, uuid, linecache, logging, requests" should be placed before "import mimetypes, time, csv, platform, base64, django, sys, os, math, uuid, linecache, logging, requests" (wrong-import-order)

what does the above line mean and why is it required?

ankit
  • 1,499
  • 5
  • 29
  • 46
  • "I the above two lines i got group the two import statements together. How can I achieve that ?" I don't understand this question. Can you please [edit] to make it more clear. – Code-Apprentice Aug 05 '18 at 17:41
  • 1
    It's not *required*. Pylint is about style issues, that's all. – Daniel Roseman Aug 05 '18 at 17:42
  • 2
    According to PEP 8, the style guide for Python, you shouldn't group multiple imports like this, put them on separate lines instead. – mklucz Aug 05 '18 at 17:46
  • Have a look at [PEP8](https://www.python.org/dev/peps/pep-0008/). Practically all Python linters check its rules, and you will get help much easier if you adhere to it. – MisterMiyagi Aug 05 '18 at 17:51
  • "It's okay to say this though: `from subprocess import Popen, PIPE`" -- pep8. The linter is also saying all imports from os should be together. It doesn't recognise os and os.path as different modules. ` – FHTMitchell Aug 05 '18 at 17:53
  • 1
    @FHTMitchell I wasn't clear, I meant this line is wrong `import mimetypes, time, csv, platform, base64, django, sys, os, math, uuid, linecache, logging, requests` Or maybe not wrong but stylistically sub-optimal. – mklucz Aug 05 '18 at 17:55
  • @mklucz I see. Apologies – FHTMitchell Aug 05 '18 at 18:03

1 Answers1

13

PEP8 suggests to order and group imports like so:

Imports should be grouped in the following order:

  1. Standard library imports.

  2. Related third party imports.

  3. Local application/library specific imports.

You should put a blank line between each group of imports.

In your case, django and requests are third party imports, and so you should write

import mimetypes, time, csv, platform, base64, sys, os, math, uuid, linecache, logging

import django, requests

It can further be useful to alphabetize imports (in each group) when you have this many.

Further, pylint seems to like grouping beyond PEP8. In particular, imports from the same module/package should be grouped together. That is, add a space between your os imports and the rest, and maybe even throw the bare os import up there as well. In all:

import os
from os import listdir
from os.path import isfile, join

import base64, csv, linecache, logging, math, mimetypes, platform, time, sys, uuid

import django, requests 
Community
  • 1
  • 1
jmd_dk
  • 12,125
  • 9
  • 63
  • 94
  • 4
    `import base64, csv, linecache, logging, math, mimetypes, platform, time, sys, uuid` this is very much discouraged by PEP8. These should all be separate import statements. PEP8: "Imports should usually be on separate lines:" – FHTMitchell Aug 05 '18 at 20:44
  • 1
    Indeed. I'm not sure if this is required by pylint though. – jmd_dk Aug 05 '18 at 20:45
  • 1
    pylint grouping means only that the imports from the same package should be in **consecutive** lines, it is *not* needed to add any *spaces* between different groups though. –  Nov 10 '20 at 13:38