Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell a Python script to halt for debugger attach to process?

Is there a way to tell Python to halt execution at a certain point in a script and wait for a debugger to attach to the process?

Is there something similar to dot-Net's Debugger.Break() in Python?

like image 333
Nitzan Avatar asked Mar 02 '16 15:03

Nitzan


People also ask

How do you stop debugging in Python?

So the most important thing to learn now — before you learn anything else — is how to quit debugging! It is easy. When you see the (Pdb) prompt, just press “q” (for “quit”) and the ENTER key. Pdb will quit and you will be back at your command prompt.

How do I run a Python script in debug mode from command line?

Directly use command python pdg-debug.py without -m pdb to run the code. The program will automatically break at the position of pdb. set_trace() and enter the pdb debugging environment. You can use the command p variable to view the variables or use the command c to continue to run.

How to debug the program in Python?

So debugging is a healthier process for the program and keeps the diseases bugs far away. Python also allows developers to debug the programs using pdb module that comes with standard Python by default. We just need to import pdb module in the Python script. Using pdb module, we can set breakpoints in the program to check the current status.

What is debugging and profiling in Python?

Debugging and profiling play an important role in Python development. The debugger helps programmers to analyze the complete code. The debugger sets the breakpoints whereas the profilers run our code and give us the details of the execution time. The profilers will identify the bottlenecks in your programs.

What is debugging in software development?

Debugging means the complete control over the program execution. Developers use debugging to overcome program from any bad issues. So debugging is a healthier process for the program and keeps the diseases bugs far away.

How to continue debugging in Linux?

To continue debugging, enter continue after the (Pdb) prompt and press Enter. If you want to know the options we can use in this, then after the (Pdb) prompt press the Tab key twice.


1 Answers

Different IDEs can use different methods for attaching to the process, however PyCharm, Eclipse, Visual Studio, and VS Code all use pydevd (VS/VSC via their Python plugin via ptvsd) to provide their in-process debugger implementation. As a result, we can target those with one piece of code.

The idea is to wait until pydevd is imported and then stop at a breakpoint.

import sys
import threading

from importlib.abc import MetaPathFinder


class NotificationFinder(MetaPathFinder):
    def find_spec(self, fullname, _path, _target=None):
        if 'pydevd' in fullname:
            with t:
                t.notify()

t = threading.Condition()
sys.meta_path.insert(0, NotificationFinder())

with t:
    t.wait()

breakpoint()

Since pydevd creates __builtins__.breakpoint, this should work regardless of the Python version. I tested in PyCharm (Community Edition 2019.1.3). I started the script, attached with the "Attach to process" option in my IDE, and was able to attach successfully. The script then stopped at breakpoint() as expected.

like image 69
Chris Hunt Avatar answered Sep 25 '22 07:09

Chris Hunt