VS Code에서 argparse
라이브러리를 사용하여 터미널에서 인자(argument)를 받는 Python 파일을 디버깅하는 방법은 다음과 같습니다:
먼저 VS Code에서 작업 중인 프로젝트 폴더를 엽니다.
터미널을 열기 위해 Ctrl +
(
또는 View
메뉴에서 Terminal
을 선택합니다.
터미널이 열리면 프로젝트 폴더로 이동합니다. 이를 위해 cd
명령어를 사용합니다. 예를 들어, cd /path/to/project
와 같이 입력합니다.
디버깅을 위해 python
명령어를 사용하여 파이썬 파일을 실행합니다. 실행할 파일에는 argparse
를 사용하여 명령행 인자를 처리하는 로직이 있어야 합니다.
예를 들어, 다음과 같이 argparse
를 사용하여 name
인자를 받는 간단한 Python 파일 hello.py
가 있다고 가정해 봅시다:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--name", help="Enter your name")
args = parser.parse_args()
print("Hello,", args.name)
위 파일을 실행하기 위해 터미널에서 다음 명령어를 입력합니다:
python hello.py --name John
이 경우, --name
옵션으로 "John" 값을 전달하고 있습니다.
VS Code에서 디버깅을 시작하기 위해 디버그 탭에서 "create a launch.json file"을 클릭하여 디버그 구성 파일을 생성합니다.
생성된 launch.json
파일을 열고, "configurations"
항목 아래에 다음 구성을 추가합니다:
{
"name": "Python: Debug argparse",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/hello.py",
"args": ["--name", "John"],
"console": "integratedTerminal"
}
위 구성에서 "program"
값은 실행할 Python 파일의 경로입니다. "args"
항목은 명령행 인자를 전달하는 역할을 합니다. 위의 예시에서는 --name
인자를 "John" 값과 함께 전달하도록 설정되어 있습니다.
launch.json
파일을 저장한 후, 디버깅을 시작합니다. 디버깅 탭에서 "Python: Debug argparse"를 선택하거나, 키보드 단축키 F5
를 눌러 디버깅을 시작합니다.
VS Code는 hello.py
파일을 디버깅 모드로 실행하며, args
변수에 명령행 인자가 올바르게 전달되었는지 확인할 수 있습니다.
위 단계를 따라하면 VS Code에서 argparse
라이브러리를 사용하여 터미널에서 인자를 받는 Python 파일을 디버깅할 수 있습니다. launch.json
파일에서 "args"
항목을 사용하여 명령행 인자를 설정하고, 디버깅 구성을 시작하여 인자가 올바르게 전달되었는지 확인할 수 있습니다.
{
// 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: Debug argparse",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/predict.py",
"args": ["--model_path", "model/kr_aes/best.th", "--vocab_path", "model/kr_aes/vocabulary", "--input_file", "data/kr/toy_txt.txt", "--output_file", "data/kr/output/aes_toy_pred_txt.txt", "--transformer_model", "monologg/kobert"],
"console": "integratedTerminal",
"justMyCode": true
}
]
}