Is it possible to debug commands executed inside the Python debugging console? - python

If I'm debugging a Python script in VS Code, while stopped at a breakpoint I have access to the Debugging Console, where I can execute Python code in the context of the current running interpreter/stack frame etc. Suppose in my code (the stuff in files, not the stuff I type in the console) I have a function function defined. I can call it from the debugging console by typing function(). However, if I have a breakpoint set inside function, VSCode does not stop at it.
Is there a way to achieve this behavior? The use case is for functions deep inside my codebase that I don't have a particularly convenient way to cause to be run by running any script. Rather than write a script that calls that function just so I have something to debug, sometimes I wish I could just start an interactive session (without even running a specific file), import and call my function interactively, and debug it from there.

Related

Equivalent of pdb.runcall in Pycharm debugger

I use pdb.runcall to check if a function behaves as I expected. However, it does not have the visual component as IDE debuggers like Pycharm's. Can I do the same thing with Pycharm debugger without running the whole program from the beginning? I want to debug a specific function by using my current environment.
Apparently, we can attach a debugger to the console with a button on the console menu (green bug to the left of the console). If we place a breakpoint in the module or function we want to step into and call the function normally the debugger lands on the breakpoint like the normal debugger. But, with the console debugger, I was able to use the variables in my environment without running the whole application.

Is it possible to debug a method called from the interactive window in PTVS?

When I'm developing in Python I often want to debug a specific method, in which case it makes sense to call the method from the interactive console or debug interactive console. However, when a method is called from the interactive windows in PTVS, it doesn't stop at the break points in said method.
If it's possible, please tell me how to do it. If not, I would like to request this feature, and also to know if there is any quicker way to debug a specific method than calling it from the main script.
I'm using PTVS 2.0 RC in Visual Studio 2013 Ultimate
When using the regular (non-debug) Python Interactive window, you can actually attach VS to the python.exe process that it is running by using Debug -> Attach to Process. Once that is done, if the interactive window does something to e.g. hit a breakpoint, the debugger will hit on that breakpoint.
The tricky part is loading the code from a file in such a way that breakpoints are resolved. In particular, $load REPL command will not work because it just reads the file and evals it in the REPL line by line, without preserving the original file context. What you need is to load your script using Python facilities - e.g. import, or open+exec.
There are also some gotchas there - e.g. the REPL window will become unresponsive whenever you are paused on a breakpoint.

In Pydev setting a breakpoint, but breakpoint not hit on callbacks only

Ok, I am new to python and my code calls some library (which is wrapping some C++ code) and I pass it a callback function on my side (as library needs to). The strange thing is that if I insert a breakpoint in my other part of the code, it will hit and deugger stops in eclipse but none of my breakpoints in the callback hit. The callback is sure called but the breakpoint is somehow ignored by PyDev. What I am doing wrong? The callback is obviously coming on a different thread. I am using Python 2.7
Try importing pdb and just manually setting breakpoints in the code with pdb.set_trace(). This won't work in all multi-threaded cases, but I find that it works in many of them and is a big improvement over the native Eclipse/PyDev debugger.

How can I debug Python code without running a script (using Eclipse)?

I've noticed how easy it is to debug a Python script from Eclipse. Simply set breakpoints and run a Python script from the debug menu. But is it possible to start a Python Interactive Interpreter instead of running a particular Python script, whilst still having Eclipse breaking on breakpoints? This would make it so much easier to test functions.
Thanks for any help
Still looking for a simple/ish way to start the debugger in Eclipse->PyDev that lets me use the interactive debugger. None of the answers as of yet is acceptable
You can explicitly write code to setup a breakpoint in your script, and then "remote debug". That means having pydevd in the pythonpath of your script wherever it is running, and running the eclipse pydev remote debugger on your devbox. If it's all happening on the same machine, this is fairly straightforward.
If not, you'll need to specify the hostname of the dev machine running the python remote debugger in the call to settrace(). You'll also need pydevd available on the machine running the script.
I have got this working in the past without having to install eclipse+pydevd on the machine running the script. It's not totally straightforward, and if you go that route I'd recommend checking that the pydevd versions match or at least you know they're compatible. Otherwise you end up wasting time debugging the debugger.
For details see: Pydev Remote Debugger
what about this, in the script, you can write a function, say onlyForTest, then each time you write a new function and want to test it, you can just put it in the onlyForTest function, then specify some arguments required by the new function you just write, then open the interactive python shell, import the script, call the onlyForTest function, check out the result.

How can I interact with rather long python scripts?

I love the IDLE. However, sometimes I have 100-200 line scripts and I want to sort of interactively debug/play with say, functions defined in foo.py instead of just calling python foo.py. Is there a way I can trigger IDLE in the context of my foo.py?
Insert this line into the script:
import pdb; pdb.set_trace()
Which will start the python debugger which lets you step through the script interactively, checking variables and such as you go.
I assume you are asking about how to enable debugging in Idle?
In the Python Shell window, choose Debugger from the Debug menu, then open foo.py and use the Run Model command. A Debug Control window opens, allowing you to step through the execution of foo.py; when execution is over, the prompt is still available for you to manually call functions, interact with objects or otherwise tinker with your application (and you will be still debugging the script).

Categories