Interactive console using Pydev in Eclipse? - python

I'm debugging my Python code in Eclipse using the Pydev plugin. I'm able to open a Pydev console and it gives me two options: "Console for currently active editor" and "Python console". However none of them is useful to inspect current variable status after a breakpoint.
For example, the code stopped at a breakpoint and I want to inspect an "action" variable using the console. However my variables are not available. How can I do things like "dir(action)", etc? (even if it is not using a console).

This feature is documented here:
http://pydev.org/manual_adv_debug_console.html

The console that opens in the debug perspective is in fact interactive, although it took me a while to realize it. You need to hit return twice after typing something and then it gets evaluated.
More info on the Pydev site here: http://pydev.org/manual_adv_debug_console.html

Double click on "action" or any other variable.
ctrl+shift+D
And if you're using watches, I cant imagine better interaction. You are able to see every change.

When I set a break point and hit F11 Eclipse launches the debugger and prompts to open the "Debug Perspective". You can then open the Window-->Show View --> Expressions which opens the expressions view, you can then right click in the Expressions view windows and choose "Add Watch Expression" to add any expression(such as dir)
Conversely I was also able to type in dir(some expression) in the PyDev console and get the same effect.
I'm using PyDev 1.4.6.2788

On a small monitor, you may not realize that the debug interactive console is different from the regular interactive console: it has a second command prompt at the bottom where you type, not at the top like the normal console.

Related

VScode run code selection

I just made the transition from Spyder to VScode for my python endeavours. Is there a way to run individual lines of code? That's how I used to do my on-the-spot debugging, but I can't find an option for it in VScode and really don't want to keep setting and removing breakpoints.
Thanks.
If you highlight some code, you can right-click or run the command, Run Selection/Line in Python Terminal.
We are also planning on implementing Ctrl-Enter to do the same thing and looking at Ctr-Enter executing the current line.
You can:
open a terminal at Terminal>New Terminal
Highlight the code you want to run
Hit Terminal>Run Selected Text
As for R you can hit CTRL Enter to execute the highlighted code. For python there's apparently no default shortcut (see below), but I am quite sure you can add yours.
In my ver of VSCode (1.25), shift+enter will run selection. Note that you will want to have your integrated terminal running python.
One way you can do it is through the Integrated Terminal. Here is the guide to open/use it: https://code.visualstudio.com/docs/editor/integrated-terminal
After that, type python3 or python since it is depending on what version you are using. Then, copy and paste the fraction of code you want to run into the terminal. It now has the same functionality as the console in Spyder. Hope this helps.
I'm still trying to figure out how to make vscode do what I need (interactive python plots), but I can offer a more complete answer to the question at hand than what has been given so far:
1- Evaluate current selection in debug terminal is an option that is not enabled by default, so you may want to bind the 'editor.debug.action.selectionToRepl' action to whatever keyboard shortcut you choose (I'm using F9). As of today, there still appears to be no option to evaluate current line while debugging, only current selection.
2- Evaluate current line or selection in python terminal is enabled by default, but I'm on Windows where this isn't doing what I would expect - it evaluates in a new runtime, which does no good if you're trying to debug an existing runtime. So I can't say much about how useful this option is, or even if it is necessary since anytime you'd want to evaluate line-by-line, you'll be in debug mode anyway and sending to debug console as in 1 above. The Windows issue might have something to do with the settings.json entry
"terminal.integrated.inheritEnv": true,
not having an affect in Windows as of yet, per vscode documentation.

Why is Python's console split in debug perspective in Eclipse with PyDev?

When I launch my Python program with the debugger in Eclipse with PyDev (Python plug-in for Eclipse), the console is always split into two windows. One where I can actually enter code and the other displays my code entries and their output. See the following snippet.
There is also a "dedicated" debug console which I can start whenever my program is paused, but this requires a manual click (PyDev > Debug Console). This console doesn't have the slip view. See snippet.
Why are there two ways to interact with Python while my program is paused? Why is the default console (from the first screenshot) split into two windows? How can I make my default console be more like the second console without the split view?
The second bottom console keeps a history of user commands that are injected into the current context. It provides a cleaner input mechanism with the ability to navigate through a history of commands. In addition some programs running loops may print to the original console frequently making it difficult to print commands into the top console.
It may have been added for future features as well, like changing the context of the input to a different spot in the code.
That is just my guess. There is a "hide console prompt" terminal button if you do not want to see it. Both consoles can have commands entered in if you are debugging and paused.

PyDev interactive console integration with Variables view (Debug perspective)

My issue with the interactive console is twofold:
When I set a breakpoint in my python code, the execution pauses as expected at the breakpoint and displays all my variables in the "Variables" view. However, the interactive console is not very interactive anymore. I would like to be able to play around with the variables when execution is still paused at the breakpoint.
Ideally I would like to have this same behaviour if I'm not debugging but just working in the interactive console. Is there a way to couple the interactive console to the "Variables" view of the "Debug" perspective. When I open an interactive console now the variables view remains empty.
I am running a fresh install of Eclipse Juno (4.4.0) with PyDev (3.7.0).
I am using the latest pydev and I find the interactive console is still interactive :-) Note that no encouraging prompt is present at the console (e.g. no ">") but if you type one of the variables you see in the variables window you will get a value.. can manipulate etc.
My terminology might be a bit lose. If you mean by interactive console the full ">" console then it is tricky to get that to work during debugging. There is a pydev variable you can set to link it to the debug session but I find it a hassle still.. you have to explicitly switch to such a console.. given a command.. it then throws you back to the normal debug console (which is the one I was referencing as still sensitive to typing variable names etc).. Perhaps I am doing something wrong though for it to be so awkward. I posted on this a few weeks back but there was no reply. I too would like to do debugging in the full console with no hassle. In particular I would like to be able to use its command history to more efficiently manipulate things.
But regardless you can still debug and look at variables just not with the full feature console easily.
Also be aware there seems to be a bug lately (last few releases) where the variables view stays blank. I find that if I close it and reopen it then the variables appear.
Good luck

python interactive console in debugger is not "interactive"

I am running Pydev 2.7.3 with Eclipse 1.5. I'm trying to use the interactive console with the debugger (i.e. what I would like to do is insert a breakpoint in a file, run the file up to that breakpoint, then muck around with stuff in the interactive debugger). It seems like what I am trying to do fits this exactly: http://pydev.org/manual_adv_debug_console.html
But the interactive console does not DO anything. It prints "pydev debugger: starting", and any prints from before the breakpoint in my file, but when I try to type commands and execute them it is not responsive. It just advances one line and does nothing when I hit enter.
What's the deal? Or how can I start looking under the hood to figure out what is going on here?
EDIT
To be clear, I'm looking for the kind of functionality described here:
Jump into a Python Interactive Session mid-program?
Interactive console using Pydev in Eclipse?
but the "interactive" console that appears (as indicated in the answers to those questions) is not interactive.
EDIT: Problem seems to have resolved. No explanation other than restarting.

Is there any way to get a REPL in pydev?

I would like to be able to drop to the python REPL from the debugger -- if this is not possible is there an easier way to evaluate python expressions in the context of the current breakpoint other than manually adding them all as watch expressions?
There is a dedicated Pydev Console available by clicking on the "New console" dropdown in the console view.
See http://pydev.sourceforge.net/console.html
I don't use pydev, but to drop to python's interactive REPL from code:
import code
code.interact(local=locals())
To drop to python's debugger from code:
import pdb
pdb.set_trace()
Finally, to run a interactive REPL after running some code, you can use python's -i switch:
python -i script.py
That will give you a python prompt after the code, even if it throws an exception.
You may be able to hook some of those solutions into pydev, I think.
As Dag Høidahl said, the PyDev Console is actually the best option (at least on Eclipse Indigo), no need to hack around.
Just go to Open Console:
Then select PyDev Console:
If you need to add specific parameters (for example, Jython tends to miss the python.os VM property), you can change them under Window -> Properties -> PyDev -> Interactive Console.

Categories