When a Python program behaves differently in VS Code than it does in your terminal, the working directory is often the reason. Relative paths such as data/input.csv, settings.json, or ./logs/app.log are resolved from the current working directory, not necessarily from the file you are debugging.
In VS Code, the Python debugger reads its launch settings from .vscode/launch.json. To choose the directory your program should start in, set the cwd property on the debug configuration.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
]
}
The value ${workspaceFolder} points to the root folder opened in VS Code. This is usually the best choice when your project has predictable folders like src, tests, data, or config. Your script will run as if you opened a terminal in the project root and launched Python from there.
If your script needs to run from a subdirectory, set cwd to that path instead. For example, a project with code under backend might use this configuration:
{
"name": "Python: Backend app",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/backend/app.py",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}/backend"
}
You can confirm the working directory from inside Python by printing os.getcwd(). That small check is helpful when debugging path problems because it shows exactly what the debugger passed to the process.
import os
from pathlib import Path
print("cwd:", os.getcwd())
print("project file exists:", Path("data/input.csv").exists())
If the printed directory is not what you expected, update cwd and start a fresh debug session. Restarting matters because the working directory is chosen when the debugged process launches. Also check whether you opened the correct folder in VS Code; opening a parent folder or only the src folder changes what ${workspaceFolder} means.
For larger projects, avoid depending too heavily on whatever the current directory happens to be. A more reliable pattern is to build paths from a known file location using __file__ and pathlib. That makes your script more portable between VS Code, terminals, tests, cron jobs, and deployment environments.
from pathlib import Path BASE_DIR = Path(__file__).resolve().parent input_path = BASE_DIR / "data" / "input.csv"
Use cwd when you need the debugger to match your normal command-line workflow. Use file-relative paths when the code itself should be independent of how it was launched. In practice, combining both habits makes Python debugging in VS Code much less surprising.