So I have this launch.json
launch.json
{
// 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": "Run main.py",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/main.py",
"console": "integratedTerminal",
"justMyCode": true,
"args": ["-o", "storage/test",
"-b", "projects/robustnav_baselines/experiments/robustnav_train", "objectnav_robothor_vanilla_rgb_augmenter_resnet_ddppo",
"-trd", "dylan/subsample_task/1_percent/train"]
}
]
}
Which essentially runs this command python main.py -o storage/test -b projects/robustnav_baselines/experiments/robustnav_train objectnav_robothor_vanilla_rgb_augmenter_resnet_ddppo -trd dylan/subsample_task/1_percent/train. When I run this command in the vs code terminal it runs fine, but when I use the debugger associated with the launch.json file. I get this exception:
allenact/init.py
Then as we can see workspaceFolder is at the root
So I'm not sure why it's not finding the allenact module even though you can clearly access it from the root as it is a folder accessible in the root with an __init__.py file.
Here is my terminal output when I run debugger. VS code will output commands in terminal when you run debugger:
cd /home/dyung6/robustnav ; /usr/bin/env /home/dyung6/anaconda3/envs/robustNav/bin/python /home/dyung6/.vscode-server/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/launcher 33361 -- /home/dyung6/robustnav/main.py -o storage/test -b projects/robustnav_baselines/experiments/robustnav_train objectnav_robothor_vanilla_rgb_augmenter_resnet_ddppo -trd dylan/subsample_task/1_percent/train
Open the "run and debug" tab from the sidebar and at the bottom you would find a section called "breakpoints".
Under that, uncheck "raised exceptions" and "user uncaught exceptions".
When I am debugging my python code from vscode, debugger shows no response and nothing on my terminal.
I have tested it with simple hello world code with a breakpoint. No output and breakpoint is never touched.
This was working fine a day before and only change I have done is installing a PYCharm(which I have uninstalled it now).
While if I execute the program without debugger then it executes without any issue.
launch.json screennshot below:
VS already updated:
Requirements.txt screenshot:
Launch.json file code
{
// 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",
"stopOnEntry": true,
"justMyCode": false
}
]
}
This is a bug in VScode. One workaround you can try is to add a requirements.txt file like this:
jedi==0.15.1
parso==0.5.1
isort==4.3.21
ptvsd==5.0.0a5
pyparsing==2.4.0
six==1.12.0
packaging==19.2
and save it to
%USERPROFILE%/.vscode\extensions\ms-python.python-2019.10.41019\
on windows
or
$HOME/.vscode/extensions/ms-python.python-2019.10.41019/
for mac and linux.
If that doesn't work, try to update the version of VScode. You may have an outdated version without the bug fixes. You could also try to add stopOnEntry and justMyCode to your launch.json file:
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
// Add these lines
"stopOnEntry": true
"justMyCode": false
},
}
You may need to add this because the default debugger will execute your file until it encounters an exception (or your program exits).
How do I run a Python program under debug and set the working directory for the run?
#SpeedCoder5's comment deserves to be an answer.
In launch.json, specify a dynamic working directory (i.e. the 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.
Per #kbro's comment, you might be prompted to create a launch.json file by clicking the Debug button itself:
When I clicked on the Debug button on my navigation panel it said "To customise Run and Debug create a launch.json file." Clicking on "create..." opened a dialog asking what language I was debugging. In my case I selected Python
Configure the cwd setting in launch.json as follows:
{
"name": "Python",
"type": "python",
"pythonPath": "python",
...
"cwd": "<Path to the directory>"
...
}
This setting helps me: (I am a Windows person)
{
"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}"
}
}
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
}
]
}
You can set up current working directory for debugged program using cwd argument in launch.json
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: Python in VSCode: Set working directory to python file's path everytime
I faced the same issue and noticed that when running the which python command in Terminal in Mac it shows me a different path to what I get when I run the which python command in vs code. And also that my file runs properly in the terminal when run using python filename.py
So I copied that path from the terminal and pasted it in VS code into Preferences->Settings->Extensions->Python->Default Interpreter Path and it worked. I hope this helps.
I use the "justMyCode = false" so I can also debug and jump into the functions that the main script calls.
{
// 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": false,
"cwd": "${fileDirname}" }
]
}
I am running a Python app in VS Code and every time I run it I have to open the file where the code starts.
Sometimes the file I am looking at is different and it is annoying to have to always remember to open that page again.
To fix this I tried to change the launch.json file but it does not work;
{
// 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
// "program": "${file}",
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/lecture1nn.py",
"console": "integratedTerminal"
}
]
}
If you're trying to have a consistent script to execute at startup, you can define a sitecustomize module which the site module will always import. That way you can have the code in sitecustomize do what you want at start-up as a side-effect. See the site module docs for details.
I am writing my first library in Python, When developing I want my run code button in VS Code to always start running the code from the main.py file in the root directory. I have added a new configuration to launch.json however I seem to be unable to use this configuration as default. How can I do this/
You need to put the 'launch.json' under the '.vscode' folder inside your workspace. Then Run > Run Without Debugging (shortcut on windows CTRL+F5)
launch.json
{
// 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": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
}
]
}
You can modify the launch.json with the below settings for key program. You may want to point program to the file which you want to execute. In the below case main.py is present in my workspace folder only. You can modify it as per your requirement.
{
// 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": "${workspaceFolder}/main.py",
"console": "integratedTerminal"
}
]
}
I found the right solution is to just change "program" in launch.json to:
"program": "main.py",
If trying to add the {workspaceFolder} it gives a FileNotFoundError.