Python has its default debugger called pdb, but there are a few alternatives created by the community. Two of them are ipdb and pdb++. They seem to cater to the same audience, they can both be run directly at the CLI and provide some niceties such as colored output and tab-completion.
Do they serve different purposes or are they simply competing debuggers with similar features?
I'm having trouble understanding when one would wish to use one over the other. There seem to even be people using both at the same time
I'm not a pdbpp expert, but to me ipdb comes with more features (IPython has many "magic" functions such as %timeit, %debug, etc that are handy), while pdbpp has a sticky mode that displays the source in the terminal as you step through (like GDB's TUI mode) and is more capable than the default pdb.
So they are both good options to debug, but my personal preference goes to ipdb because I'm used to work interactively from IPython. I've found I could set my main debugger to ipdb like so:
export PYTHONBREAKPOINT=ipdb.set_trace # or ipdb.sset_trace
And put breakpoint() in the Python source to debug. If pdbpp is also installed, I can invoke sticky from the ipdb session, which is neat.
Related
I guess this is kind of a weird question, but let's say you run a code in python that does something computationally expensive, like image processing. Oh I'm running Ubuntu 12.04 by the way. So I'm running a code, and open another terminal and type top to see what's doing what. This is ok as it tells me that python is doing its job, but what if I want to see which line is being run on the code? Is this possible? More importantly is it worth it to get this information? I can post a sample code of some of the processing if necessary
Don't blink, unless your "line of code" is unbelievably slow there is no way for such a thing to be useful. What you probably want is a Python Profiler. I suggest you start looking in http://docs.python.org/2/library/profile.html for info related to profiling your python code.
It usually is very slow but you can trace you code:
python -m trace --count -C . somefile.py ...
More manual but traditional way is logging: you can insert print statements before and after slow operations.
You can find slow places in you code using a profiler.
And you can run your code step by step with a debugger. Just insert import pdb; pdb.set_trace() (or ipdb if you like ipython) before slow operation.
This is the classic use case for a debugger. Have a look at Eclipse with the PyDev plugin, which is an IDE for Python with a useful debugger integration.
For example, a debugger allows you to set breakpoints where the execution will stop in order to let you manually step through the relevant lines of code to see how it goes. At the same time, you can inspect the variables' contents. You will thereby get a better understanding of what is happening, where and why it fails, and so on.
Go and get yourself a debugger!
I use Notepad++ for writing and running Python scripts. It is a great text editor, except for debugging. Is there a way to step through the code, use break points, view variable values etc. in Notepad++ like you can in Visual Studio?
Does such a plug-in exist? Not that I know of. I agree completely with qor72 on that note.
Is it possible to create such a plugin / functionality? Possibly.
After doing some quick digging, I did find a plugin that looks promising, Python Script. In short it allows you to run python scripts that can access the NPP modules (file menus etc...) as well as the Scintilla Methods which appear to give access to things like the markers on the pages.
To accomplish such a feat I could see the task being broken into a few large blocks (I feel a new open-source project coming on...)
Using Python Script, integrate the python debugger(PDB) as mentioned by Shashi.
Using the Scintilla Methods, add trace back calls where a NPP marker is placed
Redirect PDB outputs and process them to show where the file is stopped (again using the Scintilla methods).
While at the newly created breakpoint and using PDB determine all of the variables in the current namespace. Take this info and dump it to a CMD window, or if you want to get fancy some GUI created with Tk / wxPython
Closing Thoughts
While I think it's possible to create such a plug in, it would be quite an undertaking. Along that line, you might be better off trying to find a different editor that has this built into it already and just create macros (or whatever the IDE calls them) to add in the things you like most about NPP.
Please note that I am a daily user of NPP and have been for many years so I definitely understand why you'd like to have the functionally added to NPP. One of my favorite things about NPP is the speed in which it opens and searches files... moving to a bloated IDE, IMO, would not be worth it to me.
My current work flow is to do all of my editing in NPP and just double click to run the modules. If it fails or goes off in the weeds, I launch IDLE to debug it.
I really hope someone tells me I'm wrong (I'd love to have that feature in Notepad++) but, Notepad++ is designed as a programmers editor, not an IDE. While it has a lot of cool functionality, that level of debugging isn't part of the core tool.
Not seeing anything in the npp-plugins either.
I think python debugger
is the best option if editor is not providing facility :)
Quick guide:
from pdb import set_trace as bp
code
code
bp()
code
code
At the (Pdb) prompt, enter s to step, p foo to print foo, and c to continue executing the code until hitting another breakpoint.
Have you thought of using Komodo.
It's open source and has ports for Windows, Linux and MAC (I think).
This may be an alternative, and if you want some advice from notepad++ users, have a look at the following post on this very site:
Komodo Edit and Notepad++ ::: Pros & Cons ::: Python dev
Some npp users here seemed to have made the switch for python editing running etc...
personally don't know much about debugging on Komodo but as it's an IDE so would be surprised if you couldn't do it easily
I don't really see why Shashi's answer hasn't been upvoted. For the link that he has given supplies a way to step through python scripts as the OP has requested.
So for all who don't know about the pdb module, upon importing it the pdb.set_trace() function allows one to step through the area of code after it. And it is very much similar to the visual studios method of debugging. While you're stepping through the code you are able to input a variety of commands.
One of them is p <expression> and that allows the user to print the current state of variables within the local and global scope.
I know it's 11 years on, and I'm a bit late to the game, and I know it's not Notepad++ but please do consider Visual Studio Code.
It's free, easy to install (both the editor itself plus any python interpreters it uses) and it's widely used and nowhere near as bloated as it's Visual Studio counterpart. It also appears to be the IDE of choice for a lot of Cisco-related course material.
Write your code, click to the left of code pane to insert your breakpoints click the Debugger icon (highlighted), and you're away:
What does ipython have that bpython lacks and vice versa? How do the two differ?
If you just want an interactive interpreter, bpython should be fine. Just use it until you miss some feature you liked about IPython.
There are lots of features that IPython offers over bpython:
Special threading options. I like -gthread for experimenting with PyGTK and -pylab for matplotlib.
direct invocation of shell commands. cd in IPython is quite useful.
Full readline library support -- I can use the keyboard shortcuts I am used to.
Module reload facility - You can do a deep reload of a module after you have changed your code. This is very useful for testing and debugging.
Run functions in the background in a separate task with %bg.
A whole parallel programming environment (not really a feature you expect from an interactive Python shell, but IPython offers it).
This list could be nearly arbitrarily continued. And of course there will be lots of features in bpython lacking from IPython, but you did not ask for those.
So just use the one that works for you!
IPython Notebook (since 0.12) is a killer feature.
I wouldn't call myself programmer, but I've started learning Python recently and really enjoy it.
I mainly use it for small tasks so far - scripting, text processing, KML generation and ArcGIS.
From my experience with R (working with excellent Notepad++ and NppToR combo) I usually try to work with my scripts line by line (or region by region) in order to understand what each step of my script is doing.. and to check results on the fly.
My question: is there and IDE (or editor?) for Windows that lets you evaluate single line of Python script?
I have seen quite a lot of discussion regarding IDEs in Python context.. but havent stubled upon this specific question so far.
Thanks for help!
If you like R's layout. I highly recommend trying out Spyder. If you are using windows, try out Python(x,y). It is a package with a few different editors and a lot of common extra modules like scipy and numpy.
The only one I've had success with is Eclipse with Pydev
It's not an IDE, but you can use pdb to debug and step through your Python code. I know Emacs has built in support for it, but not so much about other editors (or IDEs) that will run in Windows.
If you are on Windows, give Pyscripter a try -- it offers comprehensive, step-through debugging, which will let you examine the state of your variables at each step of your code.
PyCharm from JetBrains has a very nice debugger that you can step through code with.
Django and console integration built in.
Rodeo seems to be new contender on the IDE market and the docs indicate that running lines of code is possible. I also have to admit it looks and behaves pretty good so far!
WingIDE, I've been using it successfully for over a year, and very pleased with it.
I use Notepad++ for most of my Windows based Python development and for debugging I use Winpdb. It's a cross platform GUI based debugger. You can actually setup a keyboard shortcut in Notepad++ to launch the debugger on your current script:
To do this go to "Run" -> "Run ..." in the menu and enter the following, making sure the path points to your winpdb_.pyw file:
C:\python26\Scripts\winpdb_.pyw "$(FULL_CURRENT_PATH)"
Then choose "Save..." and pick a shortcut that you wish to use to launch the debugger.
PS: You can also setup a shortcut to execute your python scripts similarly using this string instead:
C:\python26\python.exe "$(FULL_CURRENT_PATH)"
The upcoming RStudio 1.2 is so good that you have to try to write some python with it. 🙌
I would plump for EMACS all round.
If you're looking for a function to run code line by line (or a region if you have one highlighted), try adding this to your .emacs (I'm using python.el and Pymacs):
;; send current line to *Python
(defun my-python-send-region (&optional beg end)
(interactive)
(let ((beg (cond (beg beg)
((region-active-p)
(region-beginning))
(t (line-beginning-position))))
(end (cond (end end)
((region-active-p)
(copy-marker (region-end)))
(t (line-end-position)))))
(python-shell-send-region beg end)))
(add-hook 'python-mode-hook
'(lambda()
(local-set-key [(shift return)] 'my-python-send-region)))
I've bound it to [shift-Return]. This is borrowed from here. There's a similar keybinding for running .R files line by line here. I find both handy.
I like vim-ipython. With it I can <ctrl>+s to run a specific line. Or several lines selected on visual modes. Take a look at this video demo.
Visual Studio and PTVS: http://www.hanselman.com/blog/OneOfMicrosoftsBestKeptSecretsPythonToolsForVisualStudioPTVS.aspx
(There is also a REPL inside VS)
The Pythonwin IDE has a built-in debugger at lets you step through your code, inspect variables, etc.
http://starship.python.net/crew/mhammond/win32/Downloads.html
http://sourceforge.net/projects/pywin32/
The package also includes a bunch of other utility classes and modules that are very useful when writing Python code for Windows (interfacing with COM, etc.).
It's also discussed in the O'Reilly book Python Programming On Win32 by Mark Hammond.
Take the hint: The basic Python Read-Execute-Print-Loop (REPL) must work.
Want Evidence?
Here it is: The IDE's don't offer much of an alternative. If REPL wasn't effective, there's be lots of very cool alternatives. Since REPL is so effective, there are few alternatives.
Note that languages like Java must have a step-by-step debugger because there's no REPL.
Here's the other hint.
If you design your code well, you can import your libraries of functions and classes and exercise them in REPL model. Many, many Python packages are documented by exercising the package at the REPL level and copying the interactions.
The Django documentation -- as one example -- has a lot of interactive sessions that demonstrate how the parts work together at the REPL prompt.
This isn't very GUI. There's little pointing and clicking. But it seems to be effective.
You need to set the keyboard shortcut for "run selection" in
Tools > Preferences > Keyboard shortcuts
Then, select the line and hit the "run selection" shortcut
Light Table was doing that for me, unfortunately it is discontinued:
INLINE EVALUTION No more printing to the console in order to view your
results. Simply evaluate your code and the results will be displayed
inline.
besides from using a completely integrated IDE with debugger for python (like with Eclipse), is there any little tool for achieving this:
when running a program, i want to be able to hook somewhere into it (similar to inserting a print statement) and call a window with an object inspector (a tree view)
after closing the window, the program should resume
It doesnt need to be polished, not even absolutely stable, it could be introspection example code for some widget library like wx. Platform independent would be nice though (not a PyObjC program, or something like that on Windows).
Any Ideas ?
Edit:
Yes, i know about pdb, but I'm looking for a graphical tree of all the current objects.
Nevertheless, here is a nice introduction on how to use pdb (in this case in Django):
pdb + Django
Winpdb is a platform independent graphical GPL Python debugger with an object inspector.
It supports remote debugging over a network, multiple threads, namespace modification, embedded debugging, encrypted communication and is up to 20 times faster than pdb.
Some other features:
GPL license. Winpdb is Free Software.
Compatible with CPython 2.3 through 2.6 and Python 3000
Compatible with wxPython 2.6 through 2.8
Platform independent, and tested on Ubuntu Jaunty and Windows XP.
User Interfaces: rpdb2 is console based, while winpdb requires wxPython 2.6 or later.
Here's a screenshot that shows the local object tree at the top-left.
(source: winpdb.org)
pdb isn't windowed, it runs in a console, but it's the standard way to debug in Python programs.
Insert this where you want to stop:
import pdb;pdb.set_trace()
you'll get a prompt on stdout.
If a commercial solution is acceptable, Wingware may be the answer to the OP's desires (Wingware does have free versions, but I don't think they have the full debugging power he requires, which the for-pay versions do provide).
Python Debugging Techniques is worth reading. and it's Reddit's comment is worth reading too. I have really find some nice debug tricks from Brian's comment. such as this comment and this comment.
Of course, WingIDE is cool (for general Python coding and Python code debugging) and I use it everyday. unlucky for WingIDE still can't embedded a IPython at now.
You can use ipython, with the %debug statement. Once your code crashes, you can add breakpoints, see objects etc. A very crude way to kickoff the debugger is to raise Exception at some line of your code, run it in ipython, the type %debug when it crashes.