In Visual Studio 2010: Go to the project properties (rigth click on the project name in the Solution Explorer, then Properties on the pop up menu). Then, under Configuration Properties / Debugging, set Working Directory to $(SolutionDir)$(Configuration)\ .
@SpeedCoder5 's comment deserves to be an answer;
Specifically, 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.
{
"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.
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.
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
}
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}"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With