How to debug a Python module run with python -m from the command line?

后端 未结 6 528
后悔当初
后悔当初 2020-12-09 15:20

I know that a Python script can be debugged from the command line with

python -m pdb my_script.py

if my_script.py is a script

6条回答
  •  春和景丽
    2020-12-09 15:56

    You can add pdb.set_trace() in your code for interactive debugging, before the code you want to debug.

    class C:    
        def __init__(self, x):
            self.x = x
    
        def inst_f(self):
            pass
    
    a = C('this is a')
    import pdb
    pdb.set_trace()
    b = C('this is b')
    
    print a.x is b.x
    

    Running this will output

    > c:\python27\tests\test.py(11)()
    -> b = C('this is b')
    (Pdb) 
    

    And let you use python debugger.

提交回复
热议问题