How to set the working directory for debugging a Python program in VS Code?

@SpeedCoder5’s comment deserves to be an answer.

In launch.json, specify a dynamic working directory (i.e. the directory where the currently-open Python file is located) using:

"cwd": "${fileDirname}"

This takes advantage of the “variables reference” feature in VS Code, and the predefined variable fileDirname.

Note as comments say, you might also need to add the purpose option:

"purpose": ["debug-in-terminal"]

“Purpose” might be required if using the play button on the top-right of the window, vs F5 or “Run and Debug” in the sidebar.

If you’re using the Python: Current File (Integrated Terminal) option when you run Python, your launch.json file might look like mine, below (more info on launch.json files here).

    "version": "0.2.0",
    "configurations": [
            "name": "Python: Current File (Integrated Terminal)",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",

    //... other settings, but I modified the "Current File" setting above ...

Remember the launch.json file controls the run/debug settings of your Visual Studio code project; my launch.json file was auto-generated by VS Code, in the directory of my current “Open Project”. I just edited the file manually to add "cwd": "${fileDirname}" as shown above.

Remember the launch.json file may be specific to your project, or specific to your directory, so confirm you’re editing the correct launch.json (see comment)

If you don’t have a launch.json file, try this:

To create a launch.json file, open your project folder in VS Code (File > Open Folder) and then select the Configure gear icon on the Debug view top bar.

Per @kbro’s comment, you might be prompted to create a launch.json file by clicking the Debug button itself:

When I clicked on the Debug button on my navigation panel it said “To customise Run and Debug create a launch.json file.” Clicking on “create…” opened a dialog asking what language I was debugging. In my case I selected Python

Leave a Comment