Skip to content

Adopt code formatting tool isort for code style? #2828

Done

Description

@MartinThoma

This goes in the same direction as #2008: isort is a small little tool to make the code style more similar.

Why import sorting?

I like it, because it makes it easier for me to read a file. isort distinguishes those kinds of imports:

  • Core library: Anything that ships with Python directly (struct, math, sys, os, ...)
  • Third party: Stuff that needs to be installed. This is helpful to be in one block in case people take single files and execute them, without installing the package
  • First party: modules defined in biopython
  • Local imports: Anything starting with a dot

Overall, isort sometimes puts multiple import statements into a single one if it imports from the same package, the alphabetical sorting makes it a tiny bit more readable.

Drawbacks

try-except

It doesn't deal with those kinds of imports.

Before:

import os
from itertools import islice

try:
    from sqlite3 import dbapi2 as _sqlite
except ImportError:
    # Not present on Jython, but should be included in Python 2.5
    # or later (unless compiled from source without its dependencies)
    # Still want to offer simple parsing/output
    _sqlite = None

from Bio.Alphabet import single_letter_alphabet
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align import MultipleSeqAlignment
from .Interfaces import SequentialAlignmentWriter

After:

import os
from itertools import islice

from Bio.Align import MultipleSeqAlignment
from Bio.Alphabet import single_letter_alphabet
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord

from .Interfaces import SequentialAlignmentWriter

try:
    from sqlite3 import dbapi2 as _sqlite
except ImportError:
    # Not present on Jython, but should be included in Python 2.5
    # or later (unless compiled from source without its dependencies)
    # Still want to offer simple parsing/output
    _sqlite = None

Examples

I used an .isort.cfg which is close to what most files here seem to do already:

[settings]
indent='    '
length_sort=0
; import_heading_stdlib=Core Library modules
; import_heading_thirdparty=Third party modules
; import_heading_firstparty=First party modules
; import_heading_localfolder=Local modules
known_third_party = common_BioSQL,common_BioSQL_online,mmtf,numpy,pairwise2_testCases,rebase_update,reportlab,requires_internet,requires_wise,search_tests_common,seq_tests_common,setuptools,sphinx,test_KGML_graphics,test_SeqIO,xbb_blast,xbb_blastbg,xbb_help,xbb_search,xbb_translations,xbb_utils,xbb_widget
multi_line_output=3
include_trailing_comma=True
force_grid_wrap=0
use_parentheses=True
line_length=88

As you can see, you can specify the headings of the import types. Sometimes, I have seen that biopython already uses some like core or builtins. I like those 4 headings, but I commented them out and didn't use them in the examples below.

Also, biopython consistenly sorts underscores first. I don't know if that is possible with isort, but I asked (isort#1170)

init.py

Before:

import os
import string
import numpy

from Bio import BiopythonExperimentalWarning


import warnings

After:

import os
import string
import warnings

import numpy

from Bio import BiopythonExperimentalWarning

MauvelO.py

Before:

import re
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord
from Bio.Align import MultipleSeqAlignment
from .Interfaces import AlignmentIterator
from .Interfaces import SequentialAlignmentWriter

After:

import re

from Bio.Align import MultipleSeqAlignment
from Bio.Seq import Seq
from Bio.SeqRecord import SeqRecord

from .Interfaces import AlignmentIterator, SequentialAlignmentWriter

Another init.py

Before

from . import StockholmIO
from . import ClustalIO
from . import NexusIO
from . import PhylipIO
from . import EmbossIO
from . import FastaIO
from . import MafIO
from . import MauveIO
from . import MsfIO

After

from . import (
    ClustalIO,
    EmbossIO,
    FastaIO,
    MafIO,
    MauveIO,
    MsfIO,
    NexusIO,
    PhylipIO,
    StockholmIO,
)

_CircularDrawer.py

Before:

from reportlab.graphics.shapes import Drawing, String, Group, Line, Circle, Polygon
from reportlab.lib import colors
from reportlab.graphics.shapes import ArcPath

# GenomeDiagram imports
from ._AbstractDrawer import AbstractDrawer, draw_polygon, intermediate_points
from ._AbstractDrawer import _stroke_and_fill_colors
from ._FeatureSet import FeatureSet
from ._GraphSet import GraphSet

from math import pi, cos, sin

After

from math import cos, pi, sin

from reportlab.graphics.shapes import (
    ArcPath,
    Circle,
    Drawing,
    Group,
    Line,
    Polygon,
    String,
)
from reportlab.lib import colors

# GenomeDiagram imports
from ._AbstractDrawer import (
    AbstractDrawer,
    _stroke_and_fill_colors,
    draw_polygon,
    intermediate_points,
)
from ._FeatureSet import FeatureSet
from ._GraphSet import GraphSet

Activity

  1. peterjc commented on Apr 1, 2020

    @peterjc
    Member

    I've looked at a few other tools in this area, and personally preferred the output from https://github.com/asottile/reorder_python_imports - in particular how it does multiple imports as one per line (rather than the brackets style here). This is also black compliant and can be enforced with flake8-import-order - see e.g. peterjc/thapbi-pict@3efc7e8

    Probably using isort with use_parentheses=False would be close...

    As with the black discussion, we need to build a consensus here.

  2. peterjc commented on May 30, 2025

    @peterjc
    Member
  3. added a commit that references this issue on Oct 21, 2026
    b588d27
  4. added a commit that references this issue on Nov 2, 2026
    5b767a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions