Python 进口订单编码标准

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22722976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:29:28  来源:igfitidea点击:

Import order coding standard

pythonpython-importstatic-analysispep8

提问by alecxe

PEP8suggests that:

PEP8建议:

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.

进口应按以下顺序分组:

  1. 标准库导入
  2. 相关第三方进口
  3. 本地应用程序/库特定导入

您应该在每组导入之间放置一个空行。

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?

有没有办法使用静态代码分析工具(如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

采纳答案by sbywater

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

当前版本的 pylint 现在执行此操作,并将其报告为错误类 C0411。

回答by alecxe

Update (2016): sbywaterhas the most recent answer.

更新(2016 年):sbywater有最新的答案。



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

找到了!(不小心,在阅读“Hacker's guide to python”时)

OpenStack Hacking Style Checksproject named hackingintroduces several unique flake8extensions. There is hacking_import_groupsamong them (related commit).

名为hacking 的OpenStack Hacking Style Checks项目引入了几个独特的flake8扩展。其中有hacking_import_groups(相关提交)。

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 hackingthe 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
      
  • 要求

    • 毒物
    • 薄片8
    • 黑客攻击(来自主分支):

      $ git clone https://github.com/openstack-dev/hacking.git
      $ cd hacking/
      $ python setup.py install
      
  • 示例中使用的文件

    • tox.ini(我们需要告诉 flake8 我们要使用自定义检查)

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

      UPD:hacking检查路径的最新版本已更改,现在是hacking.checks.imports.hacking_import_groups.

    • test.py(检查对象)

      import requests
      import sys
      from my_module import print_smth
      
      
      print_smth(requests.get('https://google.com'))
      print_smth(sys.version)
      
    • my_module.py(使用的本地导入test.py

      def print_smth(smth):
          print smth
      

Then, if I run flake8against test.py:

然后,如果我flake8反对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:

然后,如果我按以下正确顺序对导入进行分组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.

希望这会在将来对某人有所帮助。

回答by cleder

Have a look at https://pypi.python.org/pypi/isortor https://github.com/timothycrosley/isort

看看https://pypi.python.org/pypi/isorthttps://github.com/timothycrosley/isort

isort parses specified files for global level import lines (imports outside of try / excepts blocks, functions, etc..) and puts them all at the top of the file grouped together by the type of import:

  • Future
  • Python Standard Library
  • Third Party
  • Current Python Project
  • Explicitly Local (. before import, as in: from . import x)

Custom Separate Sections (Defined by forced_separate list in configuration file) Inside of each section the imports are sorted alphabetically. isort automatically removes duplicate python imports, and wraps long from imports to the specified line length (defaults to 80).

isort 解析全局级别导入行的指定文件(在 try / excepts 块、函数等之外的导入),并将它们全部放在文件的顶部,按导入类型分组在一起:

  • 未来
  • Python标准库
  • 第三者
  • 当前的 Python 项目
  • 显式本地(. 导入前,如: from . import x)

自定义单独部分(由配置文件中的 force_separate 列表定义)在每个部分内,导入按字母顺序排序。isort 自动删除重复的 python 导入,并将 long 从导入包装到指定的行长度(默认为 80)。

https://pypi.python.org/pypi/flake8-isortplugs this functionality into flake8

https://pypi.python.org/pypi/flake8-isort将此功能插入 flake8

回答by phoenix

A flake8plugin exists: flake8-import-order.

flake8存在一个插件:flake8-import-order

This package adds 3 new flake8 warnings

I100: Your import statements are in the wrong order.

I101: The names in your from import are in the wrong order.

I201: Missing newline between sections or imports.

这个包添加了 3 个新的 flake8 警告

I100:您的导入语句顺序错误。

I101:从导入中的名称顺序错误。

I201:部分或导入之间缺少换行符。