4
\$\begingroup\$

I did a quick google search, and it didn't show up.

I haven't noticed it posted in pylint.py, or PEP8.py.

My emacs mode doesn't warn me if my imports are listed arbitrarily...does anyone do this?

import longest.modulename.first
import medium.sizename
import short
import sys
import os

from imports import go_at_the_bottom
from length import does_matter
from still import cascading

This is typically how I do it. But after a while, the list gets big!

I think this might work better, but it's kind of ugly:

import longest.modulename.first
import medium.sizename
import os
import short
import sys

from BeautifulSoup import BeautifulSoup as BS
from BeautifulSoup import BeautifulStoneSoup as BSS #froms are sorted by import
from imports import go_at_the_bottom
from length import does_matter
from mechanize import Browser as br
from still import cascading

What's the standard for this? I like the look of the cascading imports, but I also like the utility of searching for what's being used in the script quickly.

Is there another approach to this that I didn't cover that you use?

\$\endgroup\$
1
  • \$\begingroup\$ Interesting ... StyleCop can handle this for C#, and yes - system imports come first. \$\endgroup\$ Commented Jun 5, 2012 at 23:27

3 Answers 3

12
\$\begingroup\$

PEP-8 says:

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.

I follow it and then sort modules alphabetically within each group without separating from from the other imports, like this:

import re
import os
from   pprint import pprint
import traceback

import lxml

from   readers    import SomeReader
from   util.stuff import (stuff_init, stuff_close, StdStuff,
                          OtherStuff, BlahBlah)
\$\endgroup\$
1
  • \$\begingroup\$ Because pprint the module and pprint the function are represented by identical strings, it's not clear whether you're alphabetizing on the module from which you're importing or the name you're actually importing. In other words, if you were importing something called mprint from pprint (i.e., importing something that comes alphabetically before os), would you place that before or after your import os? \$\endgroup\$ Commented Jan 28, 2020 at 23:14
3
\$\begingroup\$

With a lot of import statements, I usually group imports by use case and origin.

#Standard library modules, grouped by use case
import os
import sys

import datetime
import time

import json



#Third party modules, also grouped by use case
import tornado.web
import tornado.ioloop



#My modules
from mymodule import something
from myothermodule import something else


#Modules used for debugging, testing etc.. 
#Stuff that will be removed before a release
import logging
import pprint



#CODE
\$\endgroup\$
1
  • \$\begingroup\$ I like this. The extra spaces are very much my style as well. \$\endgroup\$ Commented Jul 15, 2012 at 17:07
3
\$\begingroup\$

I tend to do it like so:

#Full Standard modules by length
import os
import sys
import smtp
import datetime

#Specific Funct afterword
from email.mime.text import MIMEText
from mymodule import myfunct1, myfunct2
\$\endgroup\$
1
  • \$\begingroup\$ But it is not very easy to find whether a module is already in the list, whereas sorting alphabetically helps with that. \$\endgroup\$ Commented May 26, 2016 at 10:05

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.