vscode에서 python 프로젝트를 진행하다보면, F5 키로 코드를 실행할 때 상대경로의 기준을 어디에 둘 것인지에 따라 기준이 달라서 에러가 날 경우가 있다.
이 때 launch.json의 설정 중 "cwd"(current working directory)를 잘 설정해줘야 문제없이 코드를 실행할 수 있다.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd":"${workspaceFolder}"
}
]
}
launch.json을 최초로 생성했을 때 "cwd":"${workspaceFolder}" 라인을 추가한 것이다. cwd에 대한 가능한 value는 아래에 보이는 predefined variables를 참고하자.
https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables
내가 자주 사용하는 cwd의 value는 다음과 같다.
${fileDirname} 현재 실행시킬 *.py 코드가 위치한 directory로 이동하여 코드 실행${workspaceFolder} 프로젝트의 경로에서 코드 실행하므로 코드에서 이용되는 상대경로의 기준은 프로젝트 경로이다.