I am currently using python in VSCode and when I press 'Start Debugging' or 'Run Without Debugging' this appears in my screen and as I am a beginner I just pressed the top one (Carbon ComponentS) to Run however this appears in my screen I don't know what I'm doing wrong, I have been trying for hours now but I cannot seem to find a solution. my launch.Json file looks like this What is the problem and how can I fix this? It seems like it is not python's fault because it acts the same when I try to run c++ files too. Thank you.
It appears like this when I press the top right triangle
In VS Code, the debugging function of python code is provided by the "Python" extension. Therefore, please install and use the "Python" extension in VS Code.
Before debugging python, we need to configure the debugging file "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": "${file}",
"console":"integratedTerminal",
}
]
}
debug:
Please refer to this document: Python debug configurations in Visual Studio Code.
I'm using VS Code with anaconda and it's python vers. is 3.6.x. I installed python 3.9 on last day. After this my anaconda stability is disturbed. When I started the VS Code it was running "conda activate" command automatically. But now it's not. Also git can't recognize now from terminal. I solved it ctrl+p and then select shell -> git bash.
But now I have another problem. I was debugging with F5 but now it says
cmd /C "C:\Users\Fordev\anaconda3\python.exe c:\Users\Fordev\.vscode\extensions\ms-python.python-2021.3.680753044\pythonFiles\lib\python\debugpy\launcher 58000 -- c:\Users\Fordev\Desktop\tkczmkmsi\.vscode\settings.json "
'cmd' is not recognized as an internal or external command,
operable program or batch file.
after this it waits and then says "timed out waiting for launcher to connect"
Now I removed my python path's from env. variables and system variables. I think now my configuration's are really complicated cause I tried all solves that i saw.
At the current, my environment variables:
User variables Path = C:\Users\Fordev\anaconda3\;C:\Users\Fordev\anaconda3\Scripts
System variables Path = C:\Users\Mp\AppData\Local\Programs\Python\Python39\;C:\Users\Fordev\anaconda3\;C:\Users\Fordev\anaconda3\Scripts
My launcher 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: Geçerli Dosya",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
My settings Json:
{
"python.pythonPath": "C:\\Users\\Fordev\\anaconda3\\python.exe"
}
How can I stabil configuration VS Code in Anaconda? I can run my python code but I can't debug it. And also for use github I have to change my shell to git bash everytime and I don't want that.
Can anybody tell me what's the problem?
According to the information you provided,
"c:\Users\Fordev\Desktop\tkczmkmsi\.vscode\settings.json"
it means that the file VS Code is currently debugging is "settings.json".
Please try to reload VS Code, then open the python file and click 'F5' to debug the python file.
I always used pdb for Python debugging before. Recently, I start using Visual Studio Code.
It looks in Visual Studio Code debugger, if I set a breakpoint(), Visual Studio Code will show variables' value at stopped position in the left window and I have to control it by a GUI bar.
So in "integratedTerminal" or "externalTerminal", I have no control by command line which is shown here and there isn't a pdb prompt popup. I kind of feel this surprises me since it hijacks pure Python stuff.
So is there a way to have both, the variables watch window and pdb prompt control? Especially in "integratedTerminal" or "externalTerminal".
Below are files under folder .vscode,
File settings.json
{
"python.pythonPath": "/Users/<USERNAME>/miniconda3/envs/<CONDA_ENV>/bin/python"
}
File launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Python Module",
"type": "python",
"python": "/Users/<USERNAME>/miniconda3/envs/<CONDA_ENV>/bin/python",
"request": "launch",
"program": "/Users/<USERNAME>/miniconda3/envs/<CONDA_ENV>/bin/<COMMAND>",
"console": "integratedTerminal",
"args": [
"hello-world"
],
"cwd": "${workspaceRoot}",
}
]
}
According to the information you described, when I use "breakpoint()" in the code, I click F5 to debug the code in Visual Studio Code. When the code stops, we can use the shortcut key Ctrl + Shift + ` to open a new terminal and enter the pdb interactive window. At this point, we can not only see the debug variable value, but also use the 'pdb' command:
Update:
This topic bugged me too, so I opened a feature request, where someone pointed the Debug-Console (in Visual Studio Code next to the terminal) out which lets you interact with Python at the point, where you're debugging. In case you don't find it directly here is a video how to do so. Once you found it, you can call arbitrary python functions in it.
I have written some python C++ extension that I want to debug while running it from python (interactively) using Visual Studio Code on Linux.
I have set-up my launch.json as follows for attach. Basically it is using my python interpreter as the program
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "/home/mike/.pyenv/versions/anaconda3/bin/python",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
Unfortunately, nothing happens. I am pretty new to Visual Studio Code for debugging, so I might be missing something trivial.
It was indeed trivial. Just attach the correct process. Keeping the answer here as it is not trivial to find on the web.
You could also use the VScode extension "Python C++ Debug". It automatically attaches the C++ debugger to the python debugger for you. Also if you don't have a launch.json file ready, it sets up all configurations you need when clicking on 'create a launch.json file'.
I have just installed VS Code and the Python extension, and I have not been able to get the debugger to work. Every time I try to use the debugger, it just skips over any breakpoints that I have set and runs the program like normal.
I am using VS Code on a Windows 10 PC with Python 3.7.3 and the Python extension installed. I followed the instructions here (https://code.visualstudio.com/docs/python/python-tutorial) to make a test folder called 'hello' in C:\python_work\hello and create a program called 'hello.py' inside that folder. hello.py is shown below. I tried using the debugger both by pressing the green arrow and by pressing F5, but neither seemed to make the debugger work properly. My 'launch.json' file is also shown below.
hello.py:
msg = "Hello World!"
print(msg) # Breakpoint
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": "${file}",
"console": "integratedTerminal",
"stopOnEntry": true
},
]
}
I expected the bottom bar to turn orange and the program to stop on the second line, allowing me to examine the local and global variables in the preview pane. Instead, the bottom bar stayed orange for 1/2 a second while the program ran as if I had pressed "Run Python File in Terminal," without stopping at the breakpoint. Please help!
Setting "justMyCode": false makes it stop at breakpoint:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Debug Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"stopOnEntry": true,
"justMyCode": false
},
]
}
If you're using a pytest-cov module you might also wanna have a look at pytest configuration settings note:
Note If you have the pytest-cov coverage module installed, VS Code doesn't stop at breakpoints while debugging because pytest-cov is using the same technique to access the source code being run. To prevent this behavior, include --no-cov in pytestArgs when debugging tests, for example by adding "env": {"PYTEST_ADDOPTS": "--no-cov"} to your debug configuration.
See an example configuration file (launch.json) below:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Tests",
"type": "python",
"request": "test",
"console": "integratedTerminal",
"justMyCode": false,
"env": {"PYTEST_ADDOPTS": "--no-cov"}
}
]
}
I experienced the same behaviour and was able to solve it by installing the virtual environment of Python in the following way:
[MyProjectFolder] \ venv
by entering the command
python -m venv [MyProjectFolder]\venv
in the console.
VS Code seems to expect exactly that folder structure.
Before I had installed the venv-folder-structure directly in my projects-folder, i.e.
[MyProjectFolder] \ Scripts
[MyProjectFolder] \ Lib
[MyProjectFolder] \ Include
[MyProjectFolder] \ pyvenv.cfg
which did not work and caused exactly the described debug problems.
just as a reference: VS Code version 1.52.1 and Python 3.8.5
Downgrading from Python 3.9 to 3.8 worked for me.
VC Code 1.56.2 ignored breakpoints running Python 3.9 64-bit on Windows 10 version 20H2. Installing Python 3.8.10 64-bit fixed the problem. I just needed to select the Python 3.8.10 interpreter within VS code and it now recognizes breakpoints. No changes to the configuration file were needed, for example, I did not need "justMyCode": false
I realize this is an old question, but my google search lead me to it. Many of the previous answers no longer apply. So I'm posting this fix for others who land here in 2021 trying to use 3.9.
I had the same problem having upgraded from Python version 3.7 to version 3.9. I later found out that the old version was 32 bit, and the new version I upgraded to was 64 bit, which apparently caused the issues. Uninstalling the 64 bit version and installing the 32 bit version of Python 3.9 solved my problem, and I was then able to use the Visual Studio Code breakpoint functionality again.
For everyone coming here looking for a solution related to breakpoints in python unittests:
If not already done add a configuration file, i.e. launch.json, as described in the vscode python debugging tutorial.
For me the following configuration settings worked, whereby setting cwd (because of src and test folders) and purpose (see also here) were essential.
"version": "0.2.0",
"configurations": [
{
"name": "Python: Test Cases",
"type": "python",
"request": "launch",
"cwd": "${workspaceFolder}/afinad/src",
"purpose": [
"debug-test"
],
"console": "integratedTerminal",
}
]
Happy debugging!
I recently found out ( 5th May 2020) that using Python 3.9 as interpreter the breakpoints were not hitting. Install 3.8 and it will start working again.
My issue was that I was putting the breakpoint on the line of the function declaration itself, so the def foo(x): line, instead of putting the breakpoint on the first non-comment line inside of the function.
If the first line of your function is a comment, the debugger will assume you meant to place the breakpoint on the line of the function declaration and move your breakpoint back, so really make sure you aren't placing your breakpoint on a comment!
Another source of no-breakpoints-working is elevate:
try: # https://pypi.org/project/elevate/
from elevate import elevate
except ModuleNotFoundError: # install it if not found
subprocess.call([sys.executable, "-m", "pip", "install", 'elevate'])
finally:
from elevate import elevate
# require elevated permissions right away...
elevate()
The elevate() command restarts the program as a new (child) process, so the entire file becomes un-breakpoint-able. From the author:
On Windows, the new process’s standard streams are not attached to the parent, which is an inherent limitation of UAC.
In windows, to debug python applications in VS Code which need to be run as administrator, a work-around is this:
Close VS Code.
Right-click VS Code icon, choose "Run as Administrator."
Linux users could likely do the same with sudo code. Now the process will be spawned initially as administrator, so the call to elevate() does nothing and the whole file is debuggable.
Another source of debugger not stopping at breakpoint: an homonym file, from another directory, is open in VS Code.
In fact, VS Code can set breakpoints on any Python file, even outside of your Python project's directory; if the path of the file does not match the one in launch.json, "program": [your-file-name-here] entry, then breakpoints are not hit.
For example: copies of Python source files are located under dist/lib, the output of setuptools (run: python setup.py bdist_wheel to generate them).
If dist/lib is not excluded through .gitignore, then its contents shows up in the search results sidebar and can be click open by mistake.
Caveat: the above does not hold if the launch.json configuration has "program": "${file}".
I (re)downloaded VS Code again: https://code.visualstudio.com/
The breakpoint got to show up on hover and function with the Run and Debug for the python file I was working on.
Additionally, I had to solve a syntax error in my code before the debugger buttons (step over/in, etc) had to go through the code lines
I have been battling with this for the last hour, and it finally worked by renaming my python file from camelCase.py to snake_case.py