아지기
아지기 풀스택 시니어 개발자

Visual Studio Code에서 python 포멧팅 black 설정


Visual Studio Code에서 python 포멧팅 black 설정

Visual Studio Code에서 python 포멧팅 black 설정

Visual Studio Code에서 python 포멧팅 black 설정하는 방법에 대해서 정리하였습니다.

black 설치

pip install black

설치 오류시 터미널을 관리자 권한으로 실행하여 설치

Visual Studio Code에서 black 설정 추가

File -> Preferences -> Settings 메뉴 이동 단축키 Ctrl + , 검색창에서 formatting provider 설정후에 콤보 박스에서 black 선택

Visual Studio Code에서 settings.json 편집

변경전

1
2
3
4
5
6
{
    "[python]": {
        "editor.defaultFormatter": "ms-python.python"
    },
    "python.formatting.provider": "none"
}

변경후

1
2
3
4
5
6
7
8
{
    "[python]": {
        "editor.defaultFormatter": "ms-python.python"
    },
    "python.formatting.provider": "black",
    "python.formatting.blackArgs": ["--line-length", "100"],
    "editor.formatOnSave": true
}

black 적용전 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def func(a: int):
    if a == 1:
        print('a:', a)
        return

    try:
        if a == 2:
            print('a:', a)
            return

        print('try')
        items = [1,2,3,4,5,6,7,8,9,10]
        print(items[1:])
    except:
        print('except')
        pass
    finally:
        print('finally')

def main():
    func(1)

    func(2)

if __name__ == '__main__':
    main()

black 적용후 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def func(a: int):
    if a == 1:
        print("a:", a)
        return

    try:
        if a == 2:
            print("a:", a)
            return

        print("try")
        items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        print(items[1:])
    except:
        print("except")
        pass
    finally:
        print("finally")


def main():
    func(1)

    func(2)


if __name__ == "__main__":
    main()

comments powered by Disqus