When debugging Python using VS Code, I am using windows terminal with PowerShell as my external terminal. For normal use, I have starting directory in PowerShell set as %USERPROFILE%
(C:\Users\username) but when running python code I need to change the starting folder to %__CD__%
(C:\Windows\system32) so that the current working directory would be the same as the python file I am running. (I found that this is the way it works thanks to testing.) This is necessary when working with files using python. However, I don't want to change the startingDirectory setting in PowerShell setting to %__CD__%
due to practicality and personal preference.
So my question is, is it possible to temporarily set the startingDirectory setting for PowerShell from VSCode just to run the code? So that when i open WT for any other non debugging reason the starting directory is %USERPROFILE%.
These are my VS Code launch configurations:
"launch": {
"configurations": [
{
"name": "Python: Integrated Terminal",
"type": "python",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"console": "integratedTerminal"
},
{
"name": "Python: External Terminal",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
},
"terminal.external.windowsExec":"wt -p cmd cmd",
This is my PowerShell configuration:
{
"colorScheme": "One Half Dark",
"commandline": "powershell.exe",
"fontFace": "Consolas",
guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
hidden": false,
name": "Windows PowerShell",
startingDirectory": "%USERPROFILE%",
tabTitle": "PowerShell"
}
Thanks for any suggestions.
Thank you Michael Z and la-dispute. Posting your suggestions as an answer to help other community members.
You can try the following two ways to fix the issue:
open the folder containing your code and save it as a workspace. Then type in CTRL+SHIFT+P
-> "Workspace Settings (JSON)"
, set the CWD for "Launch" within that workspace to the target directory
To configure the powershell profile type
$profile
New-Item -path $profile -type file –force
In the terminal window, it will give you a link for the PowerShell profile location. Open that file and add
set-location "file path, for example C:\Windows\system32"
save the file and reload the vscode!
You can refer to Managing Current Location, How to quickly change shell folder to match the currently open file and vscode does not set current directory in the terminal if a powershell $PROFILE also sets current directory