Search code examples
pythonpython-3.xvscode-debugger

Can python breakpoint() be disabled in a script (with VSCode debugger)?


I'm trying to use the new (python 3.7+) breakpoint(). During initial devlopment, I want to be able to disable all breakpoints within the script. Is this possible?

I tried setting PYTHONBREAKPOINT environmental variable to 0 at the top of the script, but the debugger still stops at each one. Does the env var have to set before the script is initialised? or does it need to be set to something else?

I'm using VSCode debugger, working in a virtual env, on windows 10.

    import os

    os.environ['PYTHONBREAKPOINT'] = "0"

    a = 1

    print(os.environ['PYTHONBREAKPOINT'])  # prints 0
    breakpoint()  # debugger still stops here

    b = 2

Edit: If i use pdb in the command line, breakpoints are disabled. So it is VSCode debugger related Edit2: Appears to be VSCode debugger specific. Accepted answer work for me, but means cannot use different dev and production environments to suppress breakpoints, have to change the code instead.


Solution

  • You can just set sys.breakpointhook, a function that is called when breakpoint() is executed:

    import sys
    
    sys.breakpointhook = lambda *x: None  # function that does nothing
    
    a = 1
    breakpoint()
    b = 2