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.
Related
I am looking for a Python debugger with the following features:
Up-to-date - I searched in the web and found debuggers that were last updated in 2009-2010. This is too old and implies that is not supported anymore...
Has GUI
Is standalone - I don't want to load a whole IDE for debugging some released application
Supports Python2.7 and Python 3.x
Can you recommend me on such a debugger?
EDIT:
From research and from the answers, it seems that there is no debugger that satisfies all the requirements:
winpdb was last updated in 2009/2010
pudb GUI is console based (Like vi editor) - not very convenient and modern
Other standalone debuggers have no GUI at all
Other debuggers are not standalone, but are a part of IDEs - not convenient for debugging released applications
If there's an update - I'll write it here...
https://pypi.python.org/pypi/pudb
or
http://winpdb.org/about/
I guess. Haven't used either though.
Maybe you will approach this http://pybee.org/bugjar/. This debugger has not all features, but it works.
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:
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.
I am trying to use GDB's reverse debugging with a Django application. I get it running in GDB, but I can't make it run backwards.
I stopped my Django app with Ctrl-Z and then entered reverse-next at the gdb prompt, getting the error message "Target multi-thread does not support this command."
Am I doing it wrong? Isn't this possible? Both?
Before you can use GDB for reverse debugging, you must tell it to record your program execution (so it can play it back) via target record command, as documented here.
I am not sure this will help you debug your Django application though -- GDB is well suited for debugging "native" code (compiled C/C++), and is not well suited for debugging "interpreted" code (in either forward or reverse direction).
RevDB
https://bitbucket.org/pypy/revdb
https://morepypy.blogspot.co.uk/2016/07/reverse-debugging-for-python.html
This project aims to allow pdb-like reverse debugging, which is likely what you want unless you are debugging the Python interpreter itself.
It is still in early stages as of 2017, and you must build from source.
How to question that does not mention GDB: Is it possible to step backwards in pdb?
Finally, GDB reverse debugging is quite immature, e.g. does not deal with AVX extensions Disable AVX-optimized functions in glibc (LD_HWCAP_MASK, /etc/ld.so.nohwcap) for valgrind & gdb record , so I strongly recommend using rr instead: https://github.com/mozilla/rr (on which RevDB claims to take inspiration from).
Related for JavaScript: How to go backwards while debugging Javascript in Chrome sources debugging?
That's an amazingly good question.
My first impulse would be to ensure I was using IPython as my shell for django and see if it's pdb support would help in this case. Pdb should have a very similar interface to gdb. As I recall, gdb is what's used to debug C/C++ programs, while django is being executed by a python interpreter. Using Pdb is here:
http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/
Also you might want to try using django-extensions, for access to the werkzeug debugging view.
Anyone know how to debug Python using PDB in Windows XP?
My system has some deadlock. I want to use PDB to track my system.
My Config: VC 2005, Python2.7
I would recommend using the Winpdb GUI debugger, which works great on Windows (and other OSes too--the name is misleading) and can attach to processes after launch. There's a great tutorial that gets you all the basics, which has been enough for me every time I've needed it, at least. Good luck!
I think the following page will help you. It's pydev with Eclipse.
http://pydev.org/manual_adv_remote_debugger.html