pydev interactive console always disappearing and other console questions - python

I've just started using pydev in Eclipse and I have a lot of questions about the way the interactive console works.
I have found out how to launch an interactive console and use it to run functions. My questions are this:
Every time I change my code and re-run it, my interactive console disappears. This is annoying as I have to reopen a console and I can't see/rerun my previous history. If I pin it, it stays, but then I can't run any code. Is there any way to keep the interactive console open all the time, but also be able to run your code? I currently spend a significant amount of my day closing and opening consoles!
How can I run a function from the interactive console, but still get the debugger to stop at breakpoints. I can use the debugger if I am running the code from a main function, but if I run the code from the console, it just skips right over break points.
thanks
Niall

Instead of clicking in "Python Run", you may press Ctrl+Alt+Enter with the desired Python file active, and the console will call execfile on it. All your previous story stays there. You can also select some part of your code and run only it.
As far as I know, you can't. Check the pdb module instead.

Not sure if the source of the problem is the same, but I encountered a similar issue in which the PyDev console kept disappearing as soon as I clicked in the script editor, even if the console's Pin Console button was clicked.
I solved this simply by double-clicking on the script's tab, which allowed the console to stay visible at all times.

Related

Script runs in Pycharm but finished without an exit code

I've been running some simple python scripts in Pycharm as I develop an app. The below described problem happens with every app within this project, while in other projects I'm not seeing this.
When I run the script, even if it's just a simple
print('Hello World.')
I do not get an exit code. It looks as if I am running the program from the Python Console, but I'm running it by right clicking on the script and choosing run.
I can rerun the script without issue, a new tab just opens in the python console window with an incremental (n) next to the filename.
What I end up doing is clicking on the red stop button which is when I do finally get the exit code. I then close the tab and move on.
In your "Run/Debug Configurations" choose "Emulate terminal ..."
Select emulate terminal in debug configs

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)

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.

Setting Breakpoints in IDLE [duplicate]

I solved this already, I'm just posting it here because I couldn't figure it out with google, or by reading the docs. I only found it by accident.
To add a breakpoint, right-click on the line that you want to add a breakpoint on and select "Set Breakpoint". The line will become highlighted. (Note that this only works when you are editing a file; it does not work at the interpreter.)
This site has a detailed tutorial about using the IDLE debugger.
On Mac, you need to Control-click instead of right clicking.
Completing the answer supplied by the OP: after setting the breakpoint - you must turn on IDLE Shell's debug mode (using debug --> debugger). When you run the program, press "Go" in the debug window that opens up and IDLE will stop at the breakpoint.
IDLE Shell is the window that appears when you hit F5 to run your Python script.
There is an open Python issue about the lack of documentation for IDLE breakpoints. Contributions welcome!
Update: The on-line documentation and the IDLE help files (for 2.7.4, 3.2.4, and 3.3.0) have been updated, with Nick's help.
You can set breakpoint before it is run.
Set the breakpoint by right clicking on the relevant line of your program
On your python shell, look for Debug - [Debug On] will be shown in your IDLE Python shell
Go back to your program and press F5(hotkey) to run the program, it will stop in the relevant break line(s)
If you want to look at some global variables or line of codes, you can check the box in the debugger
Just adding to this answer (would've commented but for the rules that say I don't have enough reputation to do so): On the Mac you must control-click. The normal "right-click" at least for the trackpad does not work to bring up the contextual menu with the option to set a breakpoint.

How do you add breakpoints to a Python program in IDLE?

I solved this already, I'm just posting it here because I couldn't figure it out with google, or by reading the docs. I only found it by accident.
To add a breakpoint, right-click on the line that you want to add a breakpoint on and select "Set Breakpoint". The line will become highlighted. (Note that this only works when you are editing a file; it does not work at the interpreter.)
This site has a detailed tutorial about using the IDLE debugger.
On Mac, you need to Control-click instead of right clicking.
Completing the answer supplied by the OP: after setting the breakpoint - you must turn on IDLE Shell's debug mode (using debug --> debugger). When you run the program, press "Go" in the debug window that opens up and IDLE will stop at the breakpoint.
IDLE Shell is the window that appears when you hit F5 to run your Python script.
There is an open Python issue about the lack of documentation for IDLE breakpoints. Contributions welcome!
Update: The on-line documentation and the IDLE help files (for 2.7.4, 3.2.4, and 3.3.0) have been updated, with Nick's help.
You can set breakpoint before it is run.
Set the breakpoint by right clicking on the relevant line of your program
On your python shell, look for Debug - [Debug On] will be shown in your IDLE Python shell
Go back to your program and press F5(hotkey) to run the program, it will stop in the relevant break line(s)
If you want to look at some global variables or line of codes, you can check the box in the debugger
Just adding to this answer (would've commented but for the rules that say I don't have enough reputation to do so): On the Mac you must control-click. The normal "right-click" at least for the trackpad does not work to bring up the contextual menu with the option to set a breakpoint.

Categories

Resources