Isort
python에서 import하는 라이브러리들을 자동으로 정렬
Commandline Example
-
--check
: 파일을 수정하지 않고 에러 체크만 한다. -
--diff
: 수정 부분을 diff 포맷으로 출력. -
--color
: 터미널 출력에 색상을 적용.
Default Sections Order
기본 정렬 순서는 다음과 같다:
- FUTURE
- STDLIB
- THIRDPARTY
- FIRSTPARTY
- LOCALFOLDER
예제는 다음과 같다:
from __future__ import absolute_import
import os
import sys
from third_party import (lib1, lib2, lib3, lib4, lib5, lib6, lib7, lib8,
lib9, lib10, lib11, lib12, lib13, lib14, lib15)
from my_lib import Object, Object2, Object3
print("Hey")
print("yo")
Configuration
Example
# isort configuration
[isort]
py_version = 39
profile = black
src_paths = recc,test,tester
skip_gitignore = True
# sections=FUTURE,STDLIB,FIRSTPARTY,THIRDPARTY,LOCALFOLDER
# import_heading_future = Futures
# import_heading_stdlib = Standard Libraries
# import_heading_thirdparty = Third-party Libraries
# import_heading_firstparty = First-party Libraries
# import_heading_localfolder = Local Libraries
실행 시:
Inline ignore
To make isort ignore a single import simply add a comment at the end of the import line containing the text isort:skip
:
or:
To make isort skip an entire file simply add isort:skip_file
to the module's doc string:
Troubleshooting
THIRDPARTY 라이브러리가 LOCALFOLDER 위치에 정렬되는 현상
정렬 시 다음과 같이 정렬된다:
# -*- coding: utf-8 -*-
import os
from typing import Any, Callable, Coroutine, List, Mapping
from reccd.daemon.daemon_client import DaemonClient
from type_serialize import deserialize
from answer_plugin_vms.packet.event_category import EventCategory
from answer_plugin_vms.storage.local_storage import LocalStorage
from numpy import ndarray
numpy는 THIRDPARTY
라서 6번째 줄에 출력됨으로 예상되지만 11번째 줄에 출력된다.
이러한 현상은 LOCALFOLDER
의 subdirectory 위치에 THIRDPARTY
라이브러리 이름이 포함되면 이러한 현상이 발생될 수 있다. 따라서 관련 폴더명은 피하는 것이 좋다.
또는 명시적으로 THIRDPARTY 라고 알려주면 된다.