用于重新排序python导入的工具
reorder-python-imports的Python项目详细描述
对python导入重新排序
用于自动重新排序python导入的工具。类似于isort
,但是
更多使用静态分析。
安装
pip install reorder-python-imports
控制台脚本
有关完整的选项集,请参阅reorder-python-imports --help
。
reorder-python-imports
将文件名作为位置参数
常用选项:
--py##-plus
:see below。--add-import
/--remove-import
:see below。--replace-import
:see below。--application-directories
:默认情况下,reorder-python-imports
假设 您的项目植根于.
。如果这不是真的,告诉它你的 导入根Live。例如,当使用流行的布局时 使用--application-directories=.:src
(注意:多条路径是分开的 使用:
)。
作为预提交挂钩
有关说明,请参见pre-commit。
样本.pre-commit-config.yaml
-repo:https://github.com/asottile/reorder_python_importsrev:v1.6.1hooks:-id:reorder-python-imports
它是做什么的?
将导入分为三个部分
importsysimportpyramidimportreorder_python_imports
变成
importsysimportpyramidimportreorder_python_imports
import
在from
导入
之前导入
fromosimportpathimportsys
变成
importsysfromosimportpath
拆分from
导入
fromos.pathimportabspath,exists
变成
fromos.pathimportabspathfromos.pathimportexists
删除重复导入
importosimportos.pathimportsysimportsys
变成
importos.pathimportsys
使用# noreorder
包含# noreorder
注释的行及其后的行将
被忽视。此外,任何出现在非空白之后的导入
将忽略非注释行。
例如,这些将不会更改:
importsystry:# not import, not whitespaceimportfooexceptImportError:pass
importsysimportreorder_python_importsimportmatplotlib# noreordermatplotlib.use('Agg')importmatplotlib.pyplotasplt
# noreorderimportsysimportpyramidimportreorder_python_imports
为什么是这种风格?
由reorder-python-imports
选择的样式只有一个目标:reduce merge
冲突。
通过每行有一个导入,多个贡献者可以 从单个模块添加/删除导入而不会导致冲突。
考虑以下导致合并冲突的示例:
# developer 1 -from typing import Dict, List+from typing import Any, Dict, List
# developer 2 -from typing import Dict, List+from typing import Dict, List, Tuple
与reorder-python-imports
:
+from typing import Any from typing import Dict from typing import List +from typing import Tuple
添加/删除导入
假设我想在我的代码库中强制absolute_import
。我可以使用:
--add-import 'from __future__ import absolute_import'
。
$ cat test.py print('Hello world')$ reorder-python-imports --add-import 'from __future__ import absolute_import' test.py Reordering imports in test.py$ cat test.py from __future__ import absolute_importprint('Hello world')
假设我不再关心支持Python2.5,我可以删除
from __future__ import with_statement
与
--remove-import 'from __future__ import with_statement'
$ cat test.py from __future__ import with_statementwith open('foo.txt', 'w') as foo_f: foo_f.write('hello world')$ reorder-python-imports --remove-import 'from __future__ import with_statement' test.py Reordering imports in test.py$ cat test.py with open('foo.txt', 'w') as foo_f: foo_f.write('hello world')
替换导入
导入可以自动替换为其他导入(如果它们提供相同的
姓名)。这对于将兼容性库分解为
作为six
(见下面的自动six
重写)。
此重写避免NameError
s,因为它仅在以下情况下发生:
- 导入的符号前后相同
- 导入是一个
from
导入
参数指定为orig.mod=new.mod
,或者使用可选的
选中属性orig.mod=new.mod:attr
。选中的属性很有用
用于重命名来自模块而不是完整模块的某些导入。
例如:
# full module move --replace-import six.moves.queue=queue # specific attribute move --replace-import six.moves=io:StringIO
删除过时的__future__
导入
cli提供了一些选项来帮助使用旧python“破釜沉舟”
通过删除__future__
自动导入的版本。每个选项都意味着
所有旧版本。
--py22-plus
:nested_scopes
--py23-plus
:generators
--py26-plus
:with_statement
--py3-plus
:division
,absolute_import
,print_function
,unicode_literals
--py37-plus
:generator_stop
删除/重写过时的six
导入
使用--py3-plus
,reorder-python-imports
还将删除/重写导入
来自six
。重写遵循与
replacing imports以上。
例如:
+import queue+from io import StringIO+from urllib.parse import quote_plus+ import six.moves.urllib.parse -from six.moves import queue-from six.moves import range-from six.moves import StringIO-from six.moves.urllib.parse import quote_plus