회사에서 주로 Django 개발을 많이 하고 있다. 이때, 사용하는 VSCode - launch.json 설정 내용을 공유하고자 한다.
1. VSCode launch.json 생성하기
- VSCode 왼쪽 메뉴 - [Run and Debug] 탭을 클릭하면 아래와 같이 출력된다.
- 위 사진과 같이 [create a launch.json] 클릭하면 아래와 같은 메뉴가 출력된다.
- [Python Debugger] 메뉴를 선택하면 아래와 같이 추가 메뉴가 출력된다.
- 위 화면에서 [Django]를 선택하면 .vscode/launch.json 파일이 자동으로 생성된다.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Django",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver"
],
"django": true,
"autoStartBrowser": false
}
]
}
2. launch.json 파일 분석
1) configuration
- configuration은 여러가지 debug 작업을 수행할 수 있는 리스트를 제공한다. 위에서 본 django debug 뿐만 아니라, 단일 Python 파일 debug 설정도 configuration 항목에서 아래와 같이 추가할 수 있다.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python Debugger: Django",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver"
],
"django": true,
"autoStartBrowser": false
},
{
"name": "Python Debugger: Current File",
"type": "debugpy",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
]
}
2) name
- name 항목은 Debug 설정명을 의미한다. 기본적으로 작성된 그대로 사용해도 무방하고, 원하면 자기가 알아보기 쉬운 이름으로 자유롭게 작성해도 된다.
3) type
- type은 VSCode에서 Python을 Debug 할 수 있도록 도와줄 Extension Type을 의미한다. 과거에는 "type": "python"이 기본(Default) 설정이었지만, 최근에는 MSFT에서 제공하는 Debugpy가 기본으로 설정된다.
- debugpy는 Python 3.8+ 버전을 지원하므로, 3.8 미만 버전으로 작업을 해야 할 경우 type에 debugpy 값을 python으로 변경하고, Python VSCode Extension 버전도 2020년 10월 이전 버전으로 설치하면 Debug가 가능해진다.
만약, Extension 버전을 낮추었음에도 내가 개발하는 환경의 Python 버전 기준으로 Debug가 되지 않는 경우, 계속해서 과거 버전으로 낮추어 테스트해보고 맞는 버전을 찾는 것을 권장한다.
4) program
- program 항목은 실행할 프로그램의 주 진입점 경로를 정의하는 곳이다. django 프로젝트 실행 시, manage.py를 사용하므로 program 항목에 manage.py를 명시하고, 추가적인 args를 정의하면 된다. 보통 ${workspaceFolder}/{program}.py로 자동 설정된다.
5) args
- args 항목은 program을 실행할 때, 인자(arguments)를 설정하는 곳으로 {"args1", "args2", ..} 형태로 작성한다. django 같은 경우, {"runserver", "8001"} 로 작성하면 8001 포트로 서버를 실행하도록 설정할 수 있다.
3. launch.json 전체 설정 내용
- 아래는 Django 개발 시, 자주 사용되는 Debug 설정 항목들이다.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "debugpy", // type: "python" or "debugpy"
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"8002"
],
"django": true,
"justMyCode": true
},
{
"name": "Python: Django Run Scripts",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runscript",
// {실행할 스크립트 파일명}
// "script1"
// "script2"
"script3",
"--script-args",
"[1234]"
],
"django": true,
"justMyCode": true
},
{
"name": "Python: Django Test Class",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"test",
// {App 이름}/{파일명}/{클래스명}
"django_server.tests.test_folder.TestCaseClass"
],
"django": true,
"justMyCode": true
},
{
"name": "Python: Django Test Method",
"type": "debugpy",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"test",
// {App 이름}/{파일명}/{클래스명}/{함수명}
// "django_server.tests.test_folder.TestCaseClass.test_function1"
"django_server.tests.test_folder.TestCaseClass.test_function2"
],
"django": true,
"justMyCode": true
},
{
"name": "Python: Current File",
"type": "debugpy",
"request": "launch",
// {실행할 파일명}
"program": "${workspaceFolder}/test.py",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
1) Python: Django
- Django 로컬에서 실행하여 Debug하는 모드이다.
2) Python: Django Run Scripts
- Django에서 개별 .py 파일을 실행할 때, 사용한다. 개별 .py를 실행하는 것과 달리 Django Run Scripts는 Django 내부 기능(ORM, 개발된 각종 Service, Repo 로직 등)을 모두 사용할 수 있는 상태에서 특정 .py를 실행하는 상당히 유용한 기능이다. 이 스크립트를 Debug 할 수 있도록 하는 모드이다.
3) Python: Django Test Class
- Django Test 코드 실행할 때, Class 단위로 실행하고, Debug할 수 있는 모드이다. TestClass에 작성된 테스트 케이스 함수들을 순차적으로 실행 가능하다.
4) Python: Django Test Method
- Django Test 코드 실행할 때, 특정 Class에 특정 함수를 실행하고, Debug할 수 있는 모드이다.
5) Python: Current File
- 현재 VSCode에서 열려있는 .py를 실행하는 모드이다. Django 내부 기능은 사용을 못하는 상태에서 script를 실행하므로, 기능적으로 제한이 있지만, Django 이외 Python 로직을 테스트하고 검증할 때 유용하게 사용할 수 있다.
- program 항목에 기본적으로 "${file}"로 설정된다. 만약, VSCode가 바라보는 파일이 .py가 아닌 경우, Debug 실행 시 오류가 발생한다.
- 특정 파일명을 고정해서 사용하고 싶은 경우에는 program 항목에 ${workspaceFolder}/{원하는 파일명}.py를 명시해서 사용하면 된다.
'Python' 카테고리의 다른 글
FastAPI, Poetry 기본 프로젝트 설정 (2) | 2024.11.23 |
---|---|
Poetry Package Manager 설치하기 (2) | 2024.11.20 |
VSCode Python Extension 정리 (1) | 2024.11.16 |