「import文の順番を一括で変更したい」
「PEP8に準拠したimport文をコーディングしたい」
このような場合には、isortが利用できます。
この記事では、isortについて解説しています。
本記事の内容
- isortとは?
- isortのシステム要件
- isortのインストール
- isortの動作確認
それでは、上記に沿って解説していきます。
isortとは?
isortは、import文を綺麗に書き直してくれるPythonライブラリです。
綺麗にとは、Pythonのコーディング規約であるPEP8に準拠しているという意味になります。
pep8-ja 1.0 ドキュメント
https://pep8-ja.readthedocs.io/ja/latest/
正直、PEP8に準拠していなくてもプログラムは動きます。
ただ、チームで開発したりする場合は重要になるでしょう。
このようなルールに厳しい組織・人も存在しますからね。
逆に、フリーダムな組織・人も存在します。
ただ、総じてコーディング規約には従った方がいいでしょう。
その場合、細かいルールを守って実行するのはそれなりに手間になります。
それを代わりにやってくれるのが、isortだということです。
コーディングが終わった後に、isortを実行します。
それだけで、PEP8に準拠したimport文の記述が実現できます。
なお、isortには次の二つの利用方法が存在しています。
- Python API
- コマンドラインツール
一般的には、コマンドラインツールでの利用になるでしょう。
以上、isortについて説明しました。
次は、isortのシステム要件を確認します。
isortのシステム要件
現時点(2021年10月末)でのisortの最新バージョンは、5.9.3となります。
この最新バージョンは、2021年7月29日にリリースされています。
サポートOSに関しては、以下を含むクロスプラットフォーム対応です。
- Windows
- macOS
- Linux
サポート対象となるPythonのバージョンは、以下となります。
- Python 3.6
- Python 3.7
- Python 3.8
- Python 3.9
isortのサポートは、次のPython公式開発サイクルにほぼ準じています。
バージョン | リリース日 | サポート期限 |
3.6 | 2016年12月23日 | 2021年12月23日 |
3.7 | 2018年6月27日 | 2023年6月27日 |
3.8 | 2019年10月14日 | 2024年10月 |
3.9 | 2020年10月5日 | 2025年10月 |
3.10 | 2021年10月4日 | 2026年10月 |
Python 3.10へのサポートは、まだ未対応のようです。
実際、まだ未対応のライブラリは多い状況ですからね。
ここまでをまとめると、isortのシステム要件はPython公式開発サイクル次第と言えます。
このPython公式開発サイクルに従っていれば、基本的には問題はないでしょう。
以上、isortのシステム要件を説明しました。
次は、isortをインストールします。
isortのインストール
検証は、次のバージョンのPythonで行います。
>python -V Python 3.9.7
まずは、現状のインストール済みパッケージを確認しておきます。
>pip list Package Version ---------- ------- pip 21.3 setuptools 58.2.0
次にするべきことは、pipとsetuptoolsの更新です。
pipコマンドを使う場合、常に以下のコマンドを実行しておきましょう。
python -m pip install --upgrade pip setuptools
では、isortのインストールです。
isortのインストールは、以下のコマンドとなります。
pip install isort
インストールは、一瞬で終わります。
では、どんなパッケージがインストールされたのかを確認しましょう。
>pip list Package Version ---------- ------- isort 5.9.3 pip 21.3 setuptools 58.2.0
依存するパッケージは、存在しません。
そのため、既存環境にでもisortを気軽に導入できます。
以上、isortのインストールについて説明しました。
最後は、isortの動作確認を行います。
isortの動作確認
isortには、次の二つの利用方法がありました。
- Python API
- コマンドラインツール
今回は、コマンドラインツールとして動作確認を行います。
おそらく、コマンドラインツールとしての利用がほとんどになるでしょう。
それでは、まずは対象となるファイルを用意します。
test.py
from my_lib import Object import os from my_lib import Object3 from my_lib import Object2 import sys from third_party import lib15, lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14 import sys from __future__ import absolute_import from third_party import lib3 print("test") print("desu")
上記のような適当にimportを記述したPythonスクリプトなら、何でもOKです。
このファイルtest.pyとして、保存します。
最もシンプルな利用方法は、以下。
>isort test.py Fixing ●●●\test.py
●●●は、ディレクトリのパスです。
上記のように表示されれば、test.pyが次のように編集されています。
from __future__ import absolute_import import os import sys from my_lib import Object, Object2, Object3 from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8, lib9, lib10, lib11, lib12, lib13, lib14, lib15) print("test") print("desu")
PEP8に準拠した並びにimportの記述に変更されています。
単純ですが、インパクトはありますね。
なお、isortコマンドのヘルプは以下となります。
使いこなそうとしたら、覚えることが多いです。
>isort -h usage: isort [-h] [-V] [--vn] [-v] [--only-modified] [--dedup-headings] [-q] [-d] [--overwrite-in-place] [--show-config] [--show-files] [--df] [-c] [--ws] [--sp SETTINGS_PATH] [--profile PROFILE] [--old-finders] [-j [JOBS]] [--ac] [--interactive] [--format-error FORMAT_ERROR] [--format-success FORMAT_SUCCESS] [--filter-files] [-s SKIP] [--extend-skip EXTEND_SKIP] [--sg SKIP_GLOB] [--extend-skip-glob SKIP_GLOB] [--gitignore] [--ext SUPPORTED_EXTENSIONS] [--blocked-extension BLOCKED_EXTENSIONS] [--dont-follow-links] [--filename FILENAME] [--allow-root] [-a ADD_IMPORTS] [--append] [--af] [--rm REMOVE_IMPORTS] [--float-to-top] [--dont-float-to-top] [--ca] [--cs] [-e] [--ff] [--fgw [FORCE_GRID_WRAP]] [-i INDENT] [--lai LINES_AFTER_IMPORTS] [--lbt LINES_BETWEEN_TYPES] [--le LINE_ENDING] [--ls] [--lss] [-m {GRID,VERTICAL,HANGING_INDENT,VERTICAL_HANGING_INDENT,VERTICAL_GRID,VERTICAL_GRID_GROUPED,VERTICAL_GRID_GROUPED_NO_COMMA,NOQA,VERTIC AL_HANGING_INDENT_BRACKET,VERTICAL_PREFIX_FROM_MODULE_IMPORT,HANGING_INDENT_WITH_PARENTHESES,BACKSLASH_GRID,0,1,2,3,4,5,6,7,8,9,10,11}] [-n] [--nis] [--ot] [--dt] [--rr] [--reverse-sort] [--sort-order SORT_ORDER] [--sl] [--nsl SINGLE_LINE_EXCLUSIONS] [--tc] [--up] [-l LINE_LENGTH] [--wl WRAP_LENGTH] [--case-sensitive] [--remove-redundant-aliases] [--honor-noqa] [--treat-comment-as-code TREAT_COMMENTS_AS_CODE] [--treat-all-comment-as-code] [--formatter FORMATTER] [--color] [--ext-format EXT_FORMAT] [--star-first] [--sd DEFAULT_SECTION] [--only-sections] [--ds] [--fas] [--fss] [--hcss] [--srss] [--fass] [-t FORCE_TO_TOP] [--combine-straight-imports] [--nlb NO_LINES_BEFORE] [--src SRC_PATHS] [-b KNOWN_STANDARD_LIBRARY] [--extra-builtin EXTRA_STANDARD_LIBRARY] [-f KNOWN_FUTURE_LIBRARY] [-o KNOWN_THIRD_PARTY] [-p KNOWN_FIRST_PARTY] [--known-local-folder KNOWN_LOCAL_FOLDER] [--virtual-env VIRTUAL_ENV] [--conda-env CONDA_ENV] [--py {all,2,27,3,35,36,37,38,39,auto}] [files ...] Sort Python import definitions alphabetically within logical sections. Run with no arguments to see a quick start guide, otherwise, one or more files/directories/stdin must be provided. Use `-` as the first argument to represent stdin. Use --interactive to use the pre 5.0.0 interactive behavior. If you've used isort 4 but are new to isort 5, see the upgrading guide: https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0.html general options: -h, --help show this help message and exit -V, --version Displays the currently installed version of isort. --vn, --version-number Returns just the current version number without the logo -v, --verbose Shows verbose output, such as when files are skipped or when a check is successful. --only-modified, --om Suppresses verbose output for non-modified files. --dedup-headings Tells isort to only show an identical custom import heading comment once, even if there are multiple sections with the comment set. -q, --quiet Shows extra quiet output, only errors are outputted. -d, --stdout Force resulting output to stdout, instead of in-place. --overwrite-in-place Tells isort to overwrite in place using the same file handle.Comes at a performance and memory usage penalty over it's standard approach but ensures all file flags and modes stay unchanged. --show-config See isort's determined config, as well as sources of config options. --show-files See the files isort will be ran against with the current config options. --df, --diff Prints a diff of all the changes isort would make to a file, instead of changing it in place -c, --check-only, --check Checks the file for unsorted / unformatted imports and prints them to the command line without modifying the file. Returns 0 when nothing would change and returns 1 when the file would be reformatted. --ws, --ignore-whitespace Tells isort to ignore whitespace differences when --check-only is being used. --sp SETTINGS_PATH, --settings-path SETTINGS_PATH, --settings-file SETTINGS_PATH, --settings SETTINGS_PATH Explicitly set the settings path or file instead of auto determining based on file location. --profile PROFILE Base profile type to use for configuration. Profiles include: black, django, pycharm, google, open_stack, plone, attrs, hug, wemake, appnexus. As well as any shared profiles. --old-finders, --magic-placement Use the old deprecated finder logic that relies on environment introspection magic. -j [JOBS], --jobs [JOBS] Number of files to process in parallel. --ac, --atomic Ensures the output doesn't save if the resulting file contains syntax errors. --interactive Tells isort to apply changes interactively. --format-error FORMAT_ERROR Override the format used to print errors. --format-success FORMAT_SUCCESS Override the format used to print success. target options: files One or more Python source files that need their imports sorted. --filter-files Tells isort to filter files even when they are explicitly passed in as part of the CLI command. -s SKIP, --skip SKIP Files that isort should skip over. If you want to skip multiple files you should specify twice: --skip file1 --skip file2. Values can be file names, directory names or file paths. To skip all files in a nested path use --skip-glob. --extend-skip EXTEND_SKIP Extends --skip to add additional files that isort should skip over. If you want to skip multiple files you should specify twice: --skip file1 --skip file2. Values can be file names, directory names or file paths. To skip all files in a nested path use --skip-glob. --sg SKIP_GLOB, --skip-glob SKIP_GLOB Files that isort should skip over. --extend-skip-glob SKIP_GLOB Additional files that isort should skip over (extending --skip-glob). --gitignore, --skip-gitignore Treat project as a git repository and ignore files listed in .gitignore. NOTE: This requires git to be installed and accesible from the same shell as isort. --ext SUPPORTED_EXTENSIONS, --extension SUPPORTED_EXTENSIONS, --supported-extension SUPPORTED_EXTENSIONS Specifies what extensions isort can be ran against. --blocked-extension BLOCKED_EXTENSIONS Specifies what extensions isort can never be ran against. --dont-follow-links Tells isort not to follow symlinks that are encountered when running recursively. --filename FILENAME Provide the filename associated with a stream. --allow-root Tells isort not to treat / specially, allowing it to be ran against the root dir. general output options: -a ADD_IMPORTS, --add-import ADD_IMPORTS Adds the specified import line to all files, automatically determining correct placement. --append, --append-only Only adds the imports specified in --add-import if the file contains existing imports. --af, --force-adds Forces import adds even if the original file is empty. --rm REMOVE_IMPORTS, --remove-import REMOVE_IMPORTS Removes the specified import from all files. --float-to-top Causes all non-indented imports to float to the top of the file having its imports sorted (immediately below the top of file comment). This can be an excellent shortcut for collecting imports every once in a while when you place them in the middle of a file to avoid context switching. *NOTE*: It currently doesn't work with cimports and introduces some extra over-head and a performance penalty. --dont-float-to-top Forces --float-to-top setting off. See --float-to-top for more information. --ca, --combine-as Combines as imports on the same line. --cs, --combine-star Ensures that if a star import is present, nothing else is imported from that namespace. -e, --balanced Balances wrapping to produce the most consistent line length possible --ff, --from-first Switches the typical ordering preference, showing from imports first then straight ones. --fgw [FORCE_GRID_WRAP], --force-grid-wrap [FORCE_GRID_WRAP] Force number of from imports (defaults to 2 when passed as CLI flag without value) to be grid wrapped regardless of line length. If 0 is passed in (the global default) only line length is considered. -i INDENT, --indent INDENT String to place for indents defaults to " " (4 spaces). --lai LINES_AFTER_IMPORTS, --lines-after-imports LINES_AFTER_IMPORTS --lbt LINES_BETWEEN_TYPES, --lines-between-types LINES_BETWEEN_TYPES --le LINE_ENDING, --line-ending LINE_ENDING Forces line endings to the specified value. If not set, values will be guessed per-file. --ls, --length-sort Sort imports by their string length. --lss, --length-sort-straight Sort straight imports by their string length. Similar to `length_sort` but applies only to straight imports and doesn't affect from imports. -m {GRID,VERTICAL,HANGING_INDENT,VERTICAL_HANGING_INDENT,VERTICAL_GRID,VERTICAL_GRID_GROUPED,VERTICAL_GRID_GROUPED_NO_COMMA,NOQA,VERTICAL_HANGING_I NDENT_BRACKET,VERTICAL_PREFIX_FROM_MODULE_IMPORT,HANGING_INDENT_WITH_PARENTHESES,BACKSLASH_GRID,0,1,2,3,4,5,6,7,8,9,10,11}, --multi-line {GRID,VERTIC AL,HANGING_INDENT,VERTICAL_HANGING_INDENT,VERTICAL_GRID,VERTICAL_GRID_GROUPED,VERTICAL_GRID_GROUPED_NO_COMMA,NOQA,VERTICAL_HANGING_INDENT_BRACKET,VER TICAL_PREFIX_FROM_MODULE_IMPORT,HANGING_INDENT_WITH_PARENTHESES,BACKSLASH_GRID,0,1,2,3,4,5,6,7,8,9,10,11} Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-grid, 5-vert-grid-grouped, 6-deprecated-alias- for-5, 7-noqa, 8-vertical-hanging-indent-bracket, 9-vertical-prefix-from-module-import, 10-hanging-indent-with- parentheses). -n, --ensure-newline-before-comments Inserts a blank line before a comment following an import. --nis, --no-inline-sort Leaves `from` imports with multiple imports 'as-is' (e.g. `from foo import a, c ,b`). --ot, --order-by-type Order imports by type, which is determined by case, in addition to alphabetically. **NOTE**: type here refers to the implied type from the import name capitalization. isort does not do type introspection for the imports. These "types" are simply: CONSTANT_VARIABLE, CamelCaseClass, variable_or_function. If your project follows PEP8 or a related coding standard and has many imports this is a good default, otherwise you likely will want to turn it off. From the CLI the `--dont-order- by-type` option will turn this off. --dt, --dont-order-by-type Don't order imports by type, which is determined by case, in addition to alphabetically. **NOTE**: type here refers to the implied type from the import name capitalization. isort does not do type introspection for the imports. These "types" are simply: CONSTANT_VARIABLE, CamelCaseClass, variable_or_function. If your project follows PEP8 or a related coding standard and has many imports this is a good default. You can turn this on from the CLI using `--order-by-type`. --rr, --reverse-relative Reverse order of relative imports. --reverse-sort Reverses the ordering of imports. --sort-order SORT_ORDER Specify sorting function. Can be built in (natural[default] = force numbers to be sequential, native = Python's built-in sorted function) or an installable plugin. --sl, --force-single-line-imports Forces all from imports to appear on their own line --nsl SINGLE_LINE_EXCLUSIONS, --single-line-exclusions SINGLE_LINE_EXCLUSIONS One or more modules to exclude from the single line rule. --tc, --trailing-comma Includes a trailing comma on multi line imports that include parentheses. --up, --use-parentheses Use parentheses for line continuation on length limit instead of slashes. **NOTE**: This is separate from wrap modes, and only affects how individual lines that are too long get continued, not sections of multiple imports. -l LINE_LENGTH, -w LINE_LENGTH, --line-length LINE_LENGTH, --line-width LINE_LENGTH The max length of an import line (used for wrapping long imports). --wl WRAP_LENGTH, --wrap-length WRAP_LENGTH Specifies how long lines that are wrapped should be, if not set line_length is used. NOTE: wrap_length must be LOWER than or equal to line_length. --case-sensitive Tells isort to include casing when sorting module names --remove-redundant-aliases Tells isort to remove redundant aliases from imports, such as `import os as os`. This defaults to `False` simply because some projects use these seemingly useless aliases to signify intent and change behaviour. --honor-noqa Tells isort to honor noqa comments to enforce skipping those comments. --treat-comment-as-code TREAT_COMMENTS_AS_CODE Tells isort to treat the specified single line comment(s) as if they are code. --treat-all-comment-as-code Tells isort to treat all single line comments as if they are code. --formatter FORMATTER Specifies the name of a formatting plugin to use when producing output. --color Tells isort to use color in terminal output. --ext-format EXT_FORMAT Tells isort to format the given files according to an extensions formatting rules. --star-first Forces star imports above others to avoid overriding directly imported variables. section output options: --sd DEFAULT_SECTION, --section-default DEFAULT_SECTION Sets the default section for import options: ('FUTURE', 'STDLIB', 'THIRDPARTY', 'FIRSTPARTY', 'LOCALFOLDER') --only-sections, --os Causes imports to be sorted based on their sections like STDLIB,THIRDPARTY etc. Within sections, the imports are ordered by their import style and the imports with same style maintain their relative positions. --ds, --no-sections Put all imports into the same section bucket --fas, --force-alphabetical-sort Force all imports to be sorted as a single section --fss, --force-sort-within-sections Don't sort straight-style imports (like import sys) before from-style imports (like from itertools import groupby). Instead, sort the imports by module, independent of import style. --hcss, --honor-case-in-force-sorted-sections Honor `--case-sensitive` when `--force-sort-within-sections` is being used. Without this option set, `--order-by-type` decides module name ordering too. --srss, --sort-relative-in-force-sorted-sections When using `--force-sort-within-sections`, sort relative imports the same way as they are sorted when not using that setting. --fass, --force-alphabetical-sort-within-sections Force all imports to be sorted alphabetically within a section -t FORCE_TO_TOP, --top FORCE_TO_TOP Force specific imports to the top of their appropriate section. --combine-straight-imports, --csi Combines all the bare straight imports of the same section in a single line. Won't work with sections which have 'as' imports --nlb NO_LINES_BEFORE, --no-lines-before NO_LINES_BEFORE Sections which should not be split with previous by empty lines --src SRC_PATHS, --src-path SRC_PATHS Add an explicitly defined source path (modules within src paths have their imports automatically categorized as first_party). -b KNOWN_STANDARD_LIBRARY, --builtin KNOWN_STANDARD_LIBRARY Force isort to recognize a module as part of Python's standard library. --extra-builtin EXTRA_STANDARD_LIBRARY Extra modules to be included in the list of ones in Python's standard library. -f KNOWN_FUTURE_LIBRARY, --future KNOWN_FUTURE_LIBRARY Force isort to recognize a module as part of Python's internal future compatibility libraries. WARNING: this overrides the behavior of __future__ handling and therefore can result in code that can't execute. If you're looking to add dependencies such as six a better option is to create a another section below --future using custom sections. See: https://github.com/PyCQA/isort#custom-sections-and-ordering and the discussion here: https://github.com/PyCQA/isort/issues/1463. -o KNOWN_THIRD_PARTY, --thirdparty KNOWN_THIRD_PARTY Force isort to recognize a module as being part of a third party library. -p KNOWN_FIRST_PARTY, --project KNOWN_FIRST_PARTY Force isort to recognize a module as being part of the current python project. --known-local-folder KNOWN_LOCAL_FOLDER Force isort to recognize a module as being a local folder. Generally, this is reserved for relative imports (from . import module). --virtual-env VIRTUAL_ENV Virtual environment to use for determining whether a package is third-party --conda-env CONDA_ENV Conda environment to use for determining whether a package is third-party --py {all,2,27,3,35,36,37,38,39,auto}, --python-version {all,2,27,3,35,36,37,38,39,auto} Tells isort to set the known standard library based on the specified Python version. Default is to assume any Python 3 version could be the target, and use a union of all stdlib modules across versions. If auto is specified, the version of the interpreter used to run isort (currently: 39) will be used.
以上、isortの動作確認を説明しました。