Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flake8: import statements are in the wrong order

PEP8 suggests that:

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 am using Flake8Lint which Sublime Text plugin for lint Python files.

My code as below:

import logging
import re
import time
import urllib
import urlparse

from flask import Blueprint
from flask import redirect
from flask import request
from flask.ext.login import current_user
from flask.ext.login import login_required

from my_application import one_module

it will show the warning as below:

import statements are in the wrong order, from my_application should be before from from flask.ext.login

but flask is the third party library, it should before my my_application import. This is why? How to fix it?

like image 244
pangpang Avatar asked Sep 28 '16 06:09

pangpang


People also ask

How do I order imports in Python?

Imports should be grouped in the following order: standard library imports. related third party imports. local application/library specific imports.

Does black sort import?

isort. isort helps to sort and format imports in your Python code. Black also formats imports, but in a different way from isort's defaults which leads to conflicting changes.


1 Answers

The flake8-import-order plugin needs to be configured to know which names should be considered local to your application.

For your example, if using a .flake8 ini file in your package root directory, it should contain:

[flake8]
application_import_names = my_application

Alternatively you can use only relative imports for application local imports:

from __future__ import absolute_import

import os
import sys

import requests

from . import (
    client
)


...
like image 136
gz. Avatar answered Oct 03 '22 04:10

gz.