Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import order coding standard

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.

Is there a way to check if the standard is violated anywhere in the package using static code analysis tools, like pylint, pyflakes, pychecker, pep8?


Example of violation:

from my_package import my_module from django.db import models import os 

Correct way to import:

import os  from django.db import models  from my_package import my_module 
like image 304
alecxe Avatar asked Mar 28 '14 20:03

alecxe


People also ask

What is import coding?

Import is when you import modules to use in your coding. If it cant find the module you probably have to install it. The easiest way to do this is to use pip. Write: pip install coding (or pip3) and it will install the module.

How do I order an import statement in Python?

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

Does order of import matter in Python?

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.

What is Isort in Python?

isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports.


2 Answers

The current version of pylint now does this, and reports it as error class C0411.

like image 101
sbywater Avatar answered Oct 01 '22 05:10

sbywater


Update (2016): sbywater has the most recent answer.


Found it! (accidentally, while reading "Hacker's guide to python")

OpenStack Hacking Style Checks project named hacking introduces several unique flake8 extensions. There is hacking_import_groups among them (related commit).

Example:

  • requirements

    • tox
    • flake8
    • hacking (from the master branch):

      $ git clone https://github.com/openstack-dev/hacking.git $ cd hacking/ $ python setup.py install 
  • files used in the example

    • tox.ini (we need to tell flake8 that we want to use a custom check)

      [hacking] local-check = hacking.core.hacking_import_groups 

      UPD: with the newest version of hacking the path to the check changed, now it is hacking.checks.imports.hacking_import_groups.

    • test.py (target of the check)

      import requests import sys from my_module import print_smth   print_smth(requests.get('https://google.com')) print_smth(sys.version) 
    • my_module.py (local import used by test.py)

      def print_smth(smth):     print smth 

Then, if I run flake8 against test.py:

$ flake8 test.py test.py:2:1: H305  imports not grouped correctly (requests: third-party, sys: stdlib) test.py:3:1: H305  imports not grouped correctly (sys: stdlib, my_module.print_smth: project) test.py:3:1: H306  imports not in alphabetical order (sys, my_module.print_smth) 

Then, if I group the imports in the correct order following PEP8:

import sys  import requests  from my_module import print_smth   print_smth(requests.get('https://google.com')) print_smth(sys.version) 

No warnings found:

$ flake8 test.py $ 

Hope this will help somebody in the future.

like image 22
alecxe Avatar answered Oct 01 '22 03:10

alecxe