Explain Codes LogoExplain Codes Logo

Vscode -- how to set working directory for debugging a Python program

python
debugging
vscode
launch-json
Anton ShumikhinbyAnton Shumikhin·Mar 10, 2025
TLDR

To adjust the working directory in VSCode for debugging within a Python program, you simply need to set the cwd property in your launch.json file:

"cwd": "/absolute/path/to/directory" // The place where the magic happens.

Steps:

  1. Locate and open your launch.json file (you can find this file in .vscode or by clicking Run > Add Configuration from the menu).
  2. Modify the "cwd" attribute to point to the desired directory path.
  3. Save your changes and debug—VSCode will now use this new working directory as it runs your code.

Decoding the launch.json

The launch.json file is akin to a roadmap for VSCode debugging sessions. It holds the reins of debug settings, detailing the path to the Python interpreter, defining the entry point of execution, and supplying environment variables. Hence, customizing this file to align with your project’s blueprint can unlock a streamlined debugging experience.

Are you juggling between Python interpreters or virtual environments? No problem, just specify a distinct pythonPath in your launch.json:

"pythonPath": "/path/to/python" // Your very own Python interpreter GPS.

Make sure this path is pointing towards the desired Python interpreter—this trick can be super handy when managing multi-root workspaces or catering to varying Python version prerequisites across different projects.

Flexible directory options: Static versus dynamic

VSCode debug configurations offer you the freedom to set a fixed directory using an absolute path or leverage dynamic variables for adaptive directory selection. Here's how you can set your working directory in relation to the current file or the workspace:

  • ${fileDirname} effectively sets up the launching pad at the directory hosting the current file.
  • ${workspaceFolder} propels you to the root of your project.

Remember, file paths will be sought relative to the cwd—make sure the context syncs with your code!

Harnessing environment variables

Akin to real-life where different scenarios call for variable settings, so does your development environment. Set up your debugging environment within launch.json via the env property:

"env": { "PYTHONPATH": "/path/to/additional/python/modules", // Aren't breadcrumbs just for birds? "OTHER_ENV_VAR": "value" }

Harness PYTHONPATH within this setting to ensure your code has the necessary Python modules at its disposal for execution—without tampering with environment variables at the global or system-level.

Embrace VSCode’s integrated terminal for debugging

Certain debugging situations call for live output feed in the integrated terminal. Enable this viewer courtesy of the console option:

"console": "integratedTerminal" // One window to rule them all!

Debugging and terminal output co-existent in the same VSCode window—now that's what we call a unified debugging experience!