You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{
// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${fileDirname}"
}
]
}
Because I set cwd to "${fileDirname}", then I am expecting that when I run the file scripts/test_chdir.py, the working directory will be scripts. However, that is not the case, and no matter what I do, the working directory is always the workspace directory, i.e. path/to/project. To demo this, the file scripts/test_chdir.py contains:
I don’t get this error if I cd into the scripts folder and then run python test_chdir.py. In fact, when I change the "program" entry in my launch.json file to be path/to/project/X.py:
{
// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "/path/to/project/X.py",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${fileDirname}"
}
]
}
which is a file that doesn't exist, I still get the same assertion error above, which means that VS code does not even check the launch.json file. What am I missing here?
Extension version: 2022.20.1
VS Code version: Code 1.74.1 (1ad8d514439d5077d2b0b7ee64d2ce82a9308e5a, 2022-12-14T10:30:51.966Z)
OS version: Windows_NT x64 10.0.22621
Modes:
Sandboxed: No
I found out what the problem is and its solution. It turns out that whether the "cwd" parameter in the launch.json file will be read or not will depend on how the Python file is run in VS code. From this answer, it seems that if the Python file is run using one of the following actions:
Run > Run Without Debugging (or Ctrl+F5)
Run > Start Debugging (or F5)
then the "cwd" parameter in the launch.json file will be read, and the correct working directory will be set.
However, if the Python file is run using either the "Run Python File" button or the "Debug Python File" button in the top-right of the editor window, then the "cwd" parameter in the launch.json file will not be read, and the working directory will not be the expected one. According to the answer linked to above, it is possible to force VS code to read the "cwd" parameter in the launch.json file when clicking on the "Debug Python File" button in the top-right of the editor window by setting the "purpose" parameter (more details here) in the launch.json file to "debug-in-terminal", as shown below:
{
// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${fileDirname}",
"purpose": ["debug-in-terminal"]
}
]
}
Type: Bug
My current workspace directory is
path/to/project
. Here is what is in this directory:Here is what is in the
launch.json
file:Because I set
cwd
to"${fileDirname}"
, then I am expecting that when I run the filescripts/test_chdir.py
, the working directory will bescripts
. However, that is not the case, and no matter what I do, the working directory is always the workspace directory, i.e.path/to/project
. To demo this, the filescripts/test_chdir.py
contains:When I run this using the “Debug Python file” button, I get the error:
I don’t get this error if I cd into the
scripts
folder and then runpython test_chdir.py
. In fact, when I change the"program"
entry in mylaunch.json
file to bepath/to/project/X.py
:which is a file that doesn't exist, I still get the same assertion error above, which means that VS code does not even check the
launch.json
file. What am I missing here?Extension version: 2022.20.1
VS Code version: Code 1.74.1 (1ad8d514439d5077d2b0b7ee64d2ce82a9308e5a, 2022-12-14T10:30:51.966Z)
OS version: Windows_NT x64 10.0.22621
Modes:
Sandboxed: No
System Info
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
A/B Experiments
The text was updated successfully, but these errors were encountered: