问题
Just a convenience question. I've been a bit spoiled with debuggers in IDEs like Visual Studio and XCode. I find it a bit clumsy to have to type import pdb; pdb.set_trace()
to set a breakpoint (I'd rather not import pdb at the top of the file as I might forget and leave it in).
Is there a simpler way of setting a breakpoint in Python code, as straightforward and unobtrusive as what you see in an IDE?
回答1:
You can run your program into pdb
from the command line by running
python -m pdb your_script.py
It will break on the 1st line, then you'll be able to add a breakpoint wherever you want in your code using the break
command, its syntax is:
b(reak) [[filename:]lineno | function[, condition]]
It is flexible enough to give you the ability to add a breakpoint anywhere.
回答2:
You can use:
from pdb import set_trace as bp
code
code
bp()
code
code
回答3:
In vim, I have a macro set up for this (in my .vimrc file):
map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>
so I can just press \b (when not in Insert Mode) and it adds in a breakpoint after the current line, or \B (note the capital) and it puts one before the current line.
which seems to work alright. Most other 'simple' programmers editors (emacs, sublimetext, etc) should have similar easy ways to do this.
Edit: I actually have:
au FileType python map <silent> <leader>b oimport pdb; pdb.set_trace()<esc>
au FileType python map <silent> <leader>B Oimport pdb; pdb.set_trace()<esc>
which turns it on only for python source files. You could very easily add similar lines for javascript or whatever other languages you use.
2019 Update (Python 3.7+)
Python 3.7+ now has the builtin breakpoint()
which can replace the previous import pdb; pdb.set_trace()
in vim. It still works the same.
回答4:
If you don't want to manually set breakpoints every time running the program (in Python 3.2+), e.g. say you want to directly create a breakpoint at line 3 and stop the execution there:
python -m pdb -c "b 3" -c c your_script.py
The following information may help:
If a file .pdbrc exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file.
Changed in version 3.2: .pdbrc can now contain commands that continue debugging, such as continue or next. Previously, these commands had no effect.
New in version 3.2: pdb.py now accepts a -c option that executes commands as if given in a .pdbrc file, see Debugger Commands.
- Source: https://docs.python.org/3.2/library/pdb.html
回答5:
I haven't tried it yet but they just implemented a new built-in called breakpoint() in Python 3.7 which means you can insert a breakpoint with one statement now:
breakpoint()
回答6:
This is how you would use pdb in the command line without implementing anything in your source code (the documentation and other online resources don't do a good job explaining this to a programmer who has only used visual debuggers in the past):
Start pdb by typing the following in a shell prompt:
python -m pdb 'python_script'
This command initializes pdb and the pdb debugger will break at the first line of your python_script and wait for an input from you:
(Pdb)
This is the interface for communicating with the debugger. Now, you can specify your commands here. Opposed to using buttons or keyboard shortcuts in visual debuggers, here you will use commands to derive the same results.
You can go to the next line in your code by command "n" (next):
(Pdb) n
Performing a next would display the line number, and the specific code in the source:
> python_script(line number)method name
-> current line in the source code
You can set a breakpoint by specifying a line number in your source code.
(Pdb) b 50
Here, the debugger is set to break at line 50. If there aren't any other breakpoints, the breakpoint at line 50 will be the first and it could be referenced by the breakpoint id which is 1 in this case. If you add more break points they will get identifiers sequentially (i.e., 2, 3 etc.)
Once a breakpoint is set, you would continue executing your program until pdb gets to the breakpoint as follows:
(Pdb) c
Once you get to a breakpoint you could go to the next line, with the n command as described before. If you want to examine the values of variables, you would execute the parameter command as follows:
(Pdb) p variable_name
If you no longer need a breakpoint, you can clear it by passing in the id of the breakpoint with the clear command:
(Pdb) clear 1
Finally, when you are done with the debugger you can exit the execution as you would exit the python command line interpreter.
(Pdb) exit()
I hope this will help anybody get started with pdb. Here is a list of commands you can use with the debugger: pdb so question and answers
回答7:
You could use an IDE which supports python debugging, or you can check out the excellent Winpdb tool. Which works on any platform and provides graphical debugging facilities to your python script.
http://winpdb.org/
回答8:
Python 3.7
has a new builtin way of setting breakpoints. Calling
breakpoint()
More here https://stackoverflow.com/a/53263117/6488361
回答9:
You can use:
- wing ide
- eclipse with the pydev plugin
- pycharms
All of the above support python debugging from inside an IDE.
回答10:
In Atom if Python plugins installed, you can just type in 'pdb
' and hit enter and the snippet will type import and trace back for you.
I've used to this now that sometimes I just type it in even if I'm editing it in vim and waiting for the dropdown to appear.
回答11:
The simplest way to run the debugger on your script is just
pdb your_script.py
Running pdb on a Linux command-line gives
usage: pdb.py scriptfile [arg] ...
来源:https://stackoverflow.com/questions/6980749/simpler-way-to-put-pdb-breakpoints-in-python-code