VSCode -- how to set working directory for debug

PythonVisual Studio-Code

Python Problem Overview


I'm starting to use vscode for Python. I have a simple test program. I want to run it under debug and I need to set the working directory for the run.

How/where do I do that?

Python Solutions


Solution 1 - Python

@SpeedCoder5 's comment deserves to be an answer;

You can specify a dynamic working directory; (i.e. whichever 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

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.

Solution 2 - Python

All you need to do is configure the cwd setting in launch.json file as follows:

{
    "name": "Python",
    "type": "python",
    "pythonPath":"python", 
    ....
    "cwd": "<Path to the directory>"
    ....
}

More information about this can be found on the official VS Code docs website.

Solution 3 - Python

This setting helps me:

{
  "type": "node",
  "request": "launch",
  "name": "Launch Program",
  "cwd": "${workspaceFolder}\\app\\js", // set directory here
  "program": "${workspaceFolder}\\app\\js\\server.js", // set start js here
}

Solution 4 - Python

In some cases, it might be also useful to set the PYTHONPATH along with the workspaceFolder:

{
	"name": "Python: Current File",
	"type": "python",
	"request": "launch",
	"program": "${file}",
	"console": "integratedTerminal",
	"cwd": "${workspaceFolder}",
	"env": {
		"PYTHONPATH": "${cwd}"
	}
}

Solution 5 - Python

I am posting this sample configuration for people who use TypeScript on Node.js

in my project my Node.js server TypeScript files are located in folder Application_ts and the compiled js files are generated in the folder named Application

because when we run our application in debug mode or start it normally we should start from Application folder which contains the js files so bellow configuration run debug from root folder where my application_ts also exists and works perfect

{
  "version": "0.2.0",
  "configurations": [
    {
        "type": "node",
        "request": "launch",
        "name": "Debug TypeScript in Node.js",
        "program": "${workspaceRoot}\\Application\\app.js",
        "cwd": "${workspaceRoot}\\Application",
        "protocol": "inspector",
        "outFiles": [],
        "sourceMaps": true
    },        
    {
        "type": "node",
        "request": "attach",
        "name": "Attach to Process",
        "port": 5858,
        "outFiles": [],
        "sourceMaps": true
    }
 ]
}

Solution 6 - Python

You can set up current working directory for debugged program using cwd argument in launch.json

Solution 7 - Python

To set current working directory to whatever file you are executing at the time:

File > Preferences > Settings > Python > Data Science > Execute in File Dir

Thanks brch: https://stackoverflow.com/questions/56776521/python-in-vscode-set-working-directory-to-python-files-path-everytime

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1443098View Question on Stackoverflow
Solution 1 - PythonThe Red PeaView Answer on Stackoverflow
Solution 2 - PythonDonView Answer on Stackoverflow
Solution 3 - PythonXinView Answer on Stackoverflow
Solution 4 - PythonCermakMView Answer on Stackoverflow
Solution 5 - PythonMJ XView Answer on Stackoverflow
Solution 6 - PythonKrzysztof CieslakView Answer on Stackoverflow
Solution 7 - PythonJakeView Answer on Stackoverflow