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 mig开发者_C百科ht 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?
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.
You can use:
from pdb import set_trace as bp
code
code
bp()
code
code
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()
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.
2021 Update
So I'm doing a lot of django these days, I'm using the ipdb debugger also, with some javascript and some HTML templating, so now have:
function InsertDebug()
if &filetype == "python"
execute "normal! oimport ipdb;ipdb.set_trace()\<esc>"
elseif &filetype == "javascript"
execute "normal! odebugger;\<esc>"
elseif &filetype == "html" || &filetype == "htmldjango"
execute "normal! o{% load debugger_tags %}{{ page\|ipdb }}\<esc>4bcw"
else
echoerr "Unknown filetype - cannot insert breakpoint"
endif
endfunction
nmap <leader>b <esc>:call InsertDebug()<CR>
so I get either the ipdb
breakpoint, or other language breakpoints in those languages. Then this is easily extendible to other languages too. I think it would be possible to use better autocommands to do this - but I couldn't get it to work as reliably as this.
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
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
Python 3.7
has a new builtin way of setting breakpoints. Calling
breakpoint()
More here https://stackoverflow.com/a/53263117/6488361
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/
You can use:
- wing ide
- eclipse with the pydev plugin
- pycharms
All of the above support python debugging from inside an IDE.
(I'd rather not import pdb at the top of the file as I might forget and leave it in).
Unlike
import pdb; pdb.set_trace()
which could leave the import part behind, this is "atomic":
__import__('pdb').set_trace()
Easiest way to keep breakpoints, aliases and other settings saved across run is by using .pdbrc in the same folder as your script and then just run your script as python -m pdb <scriptname>.py
Enter commands just as you would do in pdb one per line. E.g.:
.\.pdbrc
--------
b 12
display var
c
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.
You could use Vim with the Python-Mode plugin or Emacs with the Elpy plugin.
These plugins give you breakpoints with easy keystrokes (\ b
in Vim and C-c C-u b
in Emacs) plus many other features from heavy-weight IDEs (code folding, refactoring, linting, etc.) - all within a light-weight terminal-based text editor.
With this snippet breakpoints can be set automatically:
import pdb
import sys
try:
from io import StringIO # Python3
except:
from StringIO import StringIO # Python2
bp = 14 # or e.g. "submodule.py:123"
sys.stdin = StringIO('break %s\ncont\n' % bp)
pdb.set_trace()
sys.stdin = sys.__stdin__ # reset to usually stdin
print(1) # lineno 14
print(2)
This approach works with Python 2 and 3.
Breakpoints could be also set via a .pdbrc
file (locally or in home) and manipulated on the fly. Still, the cont
command must not executed manually.
So since pdb.set_trace
requires stdin, the above code just "redirects" the command to stdin in a programmatic way.
Putting the code inside the main program and combined with argparse
, the breakpoints can be passed from the command line with further stop until the breakpoint.
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] ...
精彩评论