PyDev program output in debug console - python

Please bear with me if this is very basic, as I'm quite new to PyDev. Is there some way to redirect the program output to a PyDev debug console? I'm referring to something like in Matlab. As of now, I have one console for the output where I cannot enter commands, and another in interactive mode. It's not the most convenient thing to keep switching between the two while debugging.
Thanks!

I believe that what you want is the interactive console:
Check http://pydev.org/manual_adv_interactive_console.html
After having it open, you can execute a file and have its tokens available with Ctrl+Alt+Enter (which is also the binding to open it... and you can do a selection and send only some of the contents to it too).

Related

Find the exact .py window/script that corresponds to the IPython console in Spyder

I would like to know if there is way to figure out (from the IPython console) the .py script that was used to run/execute the python commands interactively and thus got printed into the Ipython console.
For eg.
From the below screenshot, looking at the 3+3 in the Ipython console, I can see that it came in when command from untitled2.py was executed.
However when the scripts get long, and IPython output gets long and you are often shifting scripts on the left side, it can be hard to keep track.
So i was wondering if there is a way i can quickly execute some command or view some setting on Ipython console that can tell me that the above line came from untitled.py.
(Spyder maintainer here) There's no way to know from which Editor you're executing some portion of code, sorry.
However, you could use dedicated consoles (under Preferences > Run > Execute in a dedicated console) to have one console per file you execute, as long as you use F5 to run each one of them.
You could edit each .py script, so the first thing it does is to print its name and functionality.
untitled2.py:
print('untitled2.py')
...

Difference Between Python's IDLE and its command line

What are the key differences between Python's IDLE and its command line environment? IDLE looks nicer, of course, and has some kind of GUI...
Moreover, is IDLE treated the same as the shell? I mean, the shell is the middle layer between the user and Python's interpreter?
They are both the same thing but, IDLE is made to write python code so its better if you can to write on IDLE. You can also try Notepad++ its a pretty good program to write code on.
I am not sure what you question is, but here is a Windows-7 oriented answer of similarity and difference. In the start menu for Python x.y, you can select 'Python x.y (x bits)' to run python interactive in a text-line-oriented console window provided by Microsoft. The console handles key presses and mouse movements and clicks. When you hit , the console sends the line of text to python, which is waiting for input on sys.stdin. When Python processes the line, it sends output to sys.stdout or sys.stderr. This includes '>>> ' and '... ' prompts. The console displays the text for you to see.
In the start menu, you can instead select 'Idle ...'. Unless you have previously selected a different startup option, python run Idle code which uses the tkinter module which used tcl/tk to run a graphical user interface that somewhat imitates the console. The tkinter/tk gui handles key and mouse input and displays output. In both cases, some software besides the Python interpreter itself handles interaction between you and Python.
Some important differences:
Cut, copy, and paste work normally. The Windows console is crippled in this respect.
Idle colors input and output. The Windows console does not.
Idle can display all unicode BMP (the first 64K) chars. The Windows console is limited by code pages.
For 1, 2, and 3, the console of other OSes may do as well or better than Idle.
Idle lets you enter, edit, send, and retrieve complete statements. Interactive python with Windows console only works with physical lines.
Update, 2017/11:
Item 1 above: At least on current Win10, cut, copy, and paste work normally.
Item 3 above: At least on Win10, unicode works better in Command Prompt with 3.6+.
New item 5: The IDLE doc section, also available as Help => IDLE Help now has section '3.3. IDLE-console differences'.
IDLE is a very simple Integrated Development Environment. It runs the same python, libraries etc. as commant-line.
Even more basic (with less features) is IPython. Full feature IDE for Python is, for example, Eclipse with PyDev plugin, or LiClipse.
Python IDLE is where you write your program/s and Python Shell is where you run your program/s.

PyDev Interactive Console Issue

So I installed PyDev in Eclipse and started testing it and I have come to an issue.
While using IDLE to run Python I could, for example, create a file, set a variable x = 10 and then make IDLE run said file. I would then be able to ask python for x and it would give me 10. I don't know how to do that in PyDev.
I created a python interactive console and then when prompted chose the "Console for currently active editor" but the console will not recognize x even though the editor has x defined to 10. I did save before creating the console, I also ran the file before I opened the console... I do not know what to do...
Thank you!
Hmmm, I am not familiar with the IDE IDLE, nor do I typically run a file via the console, but maybe I understand your question. The core answer is you need a breakpoint so that execution does not terminate and therefore x=10 is resident in memory. If the breakpoint is set post x=10, then when you reach the breakpoint and execution stops and you type "x" you will get 10.
There is documentation online on the console about how to use it in context of a load of file from with the console. I tend instead to hit shift-F9 while in the file to run it in debug mode. This leaves you in debug console rather than interactive console (you'll see no prompting ">") but you'll still be able to have x=10 when you enter x at break.
Probably misunderstood but though I would give it a shot. Good luck!
After creating the console you have to do an execfile of the python in the console... (ctrl+alt+enter).
See: http://pydev.org/manual_adv_interactive_console.html for details
Note that I believe the current version has an issue with that command -- but I'll probably do a release tomorrow which has a fix for that (PyDev 3.8)

Has PyDev an interactive shell (during debugging) as in Komodo?

so far I used the Komodo IDE for Python development, but I'm now testing Eclipse with PyDev. Everything works fine, but there is one Komodo feature that I'm missing.
In Komodo I can inspect the running application in a debugger shell. I.e. after hitting a breakpoint I can not only read the content of variables, but I can execute arbitrary Python code (e.g. changing the value of variables) and then continue program execution.
PyDev has also some interactive shell during debugging, but I can only read variables and not change their content. Is this feature not available in PyDev or am I missing something here?
Many thanks,
Axel
As you've seen, you can just use the console directly:
http://pydev.org/manual_adv_debug_console.html
Now, you can also connect the interactive console (which is a bit more advanced) by selecting a stack frame to attach it:
http://pydev.org/manual_adv_interactive_console.html
Yes you can do that. Just type in the console what ever commands you want :). I usually have to right click then
Debug As >> Python run
PyDev is a little bit quirky, but you get used to it.

Running code in PyCharm's console

Are there any smooth way to run Python scripts in the PyCharm's console?
My previous IDE - PyScripter - provides me with that nice little feature. As far as I know PyCharm has 2 ways of running script in console:
1) Select a bunch of code and press Ctrl+Alt+E.
2) Save the code in a file and import it from the Console.
Are the any way to do it by pressing "Run" or "Debug" buttons? I need to see the result of my script in the console and all variables available to manipulate.
In the Run/Debug Configuration, add -i to the interpreter options. This will stop it from closing a python session even after a successful run. I.e. you will be able to see all the variable contents
Run -> Edit configuration -> select the script you want to run and give it a display name -> OK
Now you can run it with the green "Run" button. The green bug button (next to the run button) will run it in debug mode.
Remark: next to the run button you can monitor the script/configuration you run by selecting it's display name.
If you create run-time configuration then after pressing the run button (green "play" icon) you will get the desired result: your code will give you output in a console which opens atomatically at the bottom.
You can create many different configuraions for run and debug within a single project.
Here is a link from PyCharm's web help which covers the run/debug configurations:
http://www.jetbrains.com/pycharm/webhelp/run-debug-configuration-python.html
A possible way of manipulating the variables using debug mode and evaluate expression window is added in comment but I missed one detail: to see the result of your interactive code execution you have to switch from Debugger to Console output; mode tabs are on the top-left side of the bottom pane.
The way I do it is a create my script in the editor, call it myfirst.py, eg
print "Hello"
a= 1234
then in the console I run:
reload (myfirst)
To check on the variables use:
>>> myfirst.a
Not perfect but that's pyCharm for you. If you want a pyScripter like experience which I think is more useful you can try spyder2.
It may also be a good option for you to use the magic command
%run scriptname.py
in the ipython console (including the %-character). Compared to Cntrl + Alt + E you also get clean information on errors in your code.
Due to autocomplete it's also typed in a second and you can use this in any environment with an ipython shell. For me as a scientist who often uses python to explore data, this is a good option.
With your program in the editor:
Run - Edit Configurations
Then under Execution check the box for "Run with Python console".
When you run the program from the green arrow it will run in a console. After the run all your objects are available to examine and play with.

Categories

Resources