PyCharm: Use python console for debugging - python

I just wanted to know if there is a way in PyCharm to use the debug console as an "all-rounder". I.e. try out commands, create variables and debug Python scripts so that the debug console still remains interactive. Spyder has exactly this feature and I have been looking for this in PyCharm for a long time.

Related

PyCharm debug console is read only, no command prompt

I am using PyCharm 2021.2.4 on macOS Big Sur 11.6 with my interpreter running Python 3.9
My PyCharm debugger console displays as read only without the command prompt. I need the prompt so I can set the proper variables when I launch the script.
Here's what my debugger console looks like:
Here's the prompt I'm expecting as per the Jet Brains documentation: https://www.jetbrains.com/help/pycharm/using-debug-console.html#open
So far, I have tried:
Verifying that "Emulate terminal in console output" is deactivated as per: https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000244824-Console-toolbar-is-gone?page=1#community_comment_115000347810
Installing PyCharm 2020.3 to check whether an earlier version exhibits the same behavior (it does).
Creating a conda environment using Python 2.7 as the interpreter.
Resetting the IDE to its default settings.
And in case it's relevant, I'm trying to run construal_level_task.py which is the file in this repo https://github.com/UOSAN/high_level_construal
At the command line, the file is launched with this command: python3 construal_level_task.py --id ASH999 --run 1 --session 1 and those inputs (id, run, session) are the ones I'm trying to enter in the debugger console to run the script in debug mode.
The python program exited, so you cannot interact with the console any more. You need to set a break point in your code. Then the debug console will become interactive.
those inputs (id, run, session) are the ones I'm trying to enter in the debugger console
Using the debug console is not the right way to enter inputs from command line arguments. Instead, you should edit the run configuration and add them there. See Pycharm and sys.argv arguments for an explanation of how to do this.
I think the problem is that you use conda interpreter. Is there a need for this? You can try using pure interpretator.
EDIT: Try to use virtual using normal interpreter (not conda). Your mistake is conflicting copies of conda envs.

how can I configure PyCharm so that running a Python script file is visible for Python Console

I am writing Python scripts in Pycharm with IPython installed. So I can use Python Console in Pycharm to type Python commands and check the immediate output of the codes. However, when I run a script file after pressing 'Run' button (Shift+F10), all the variables and functions are not visible to the Python Console. This is, however, the feature of Spyder, another popular Python IDE. So here is my question: how can I configure Pycharm so that running a Python script file is visible for Python Console? Thanks
You could also run the part of your code you want to test/check in the console by selecting it and then right clicking and clicking on "Execute Selection in Console Alt-Shift-E". That's what I use sometimes when the debugger is not helpful. After running the code (you can also just "run" functions or classes) the console knows the functions and you can use the same features that Spyder has. However, be aware that when you change the code you need to run it in the console once to update the console definitions!
You can not. But you can use pdb (which will break code execution where you need it and you will be able to do the same things, as in the Python Console).
And, which is better and more powerful, you can use PyCharm's debugger. It represents all available variables in tree-like structures and is really handy.

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.

Output of Python script not shown in Eclipse console

Using Eclipse Juno under Windows I have configured an external tool that calls the Python.exe program, with a Python script file to run as argument (the latter in between double quotes, like "C:\script.py").
I have also double checked that in the external tools configuration window, in the "Common" tab the option "Allocate console" is set.
However, when I now run this external tool, no Python output at all is shown in the Eclipse console.
On the other hand, when I define another external tool, calling cmd.exe, then I can see all output from that shell in the Eclipse console fine.
Moreover, when I run python.exe "C:\script.py" directly from a Windows shell window, then I can also see all output comming from this Python script. Also when I run the Python script in Eclipse using PyDev directly (where I have developed the script), then again its output is shown nicely in the Eclipse/PyDev console.
So, the Eclipse console does theoretically work fine with external tools, and the called Python script does print output. But running the Python script as external tool does not show any output in the Eclipse console.
Any ideas?
This might be a limitation of Windows that some output cannot be read other programs. I don't understand it myself yet, but it might be related to this.

Python and Eclipse: How to use interactive console after running a module, with the module variables and functions

Let's consider 3 situations:
1) I write a pyhon module in Eclipse (pydev) and run it Ctrl-F11. The module runs and I don't have any control or access (AFAIK) to the module variables and functions.
2) I have defined a python interpreter as an external tool in Eclipse, so I can run it within Eclipse. It works fine, but it does not have tab completion.
3) I open my module with python IDLE (not eclipse) and press f5 (run). It runs on the IDLE opened window and when it finishes I have all the variables and functions form the module to play with.
So I have 2 questions:
a) how to enable, if possible, tab completion in python interpreter in Eclipse? If it's not possible, do I have any alternative to standard Python interpreter?
b) I would like to run step 1) and then be able to continue developing / testing with python interpreter, just like I do with IDLE, but all inside Eclipse. Any suggestion?
Thanks in advance
Not sure how much has changed since the question was asked, but it's now possible to get code completion in the debug console (as of PyDev 1.6.0 according to http://pydev.org/manual_adv_debug_console.html, I'm on PyDev 2.8.1, Eclipse 4.3.0)
Set a breakpoint in the code, on an executable statement (even just print('') or True). Double clicking on the grey vertical bar to the left of the code should do it.
Run in debug mode with F11
Eclipse may prompt to switch to the Debug Perspective; either way, the Console should be visible
You should now be able to click in the Console pane and begin interactive debugging with code completion, even after a traceback. (Ctrl+Space works for me.)
Be aware that multi-line statements won't work unless they're split on : or /:
Update in 1.6.0: commands are evaluated on each new line unless the line starts with ' ' or '/t' or ends with ':' or '/' (so, for entering multi-line statements, the input must be entered properly respecting those limitations).
If you run it as a debug operation in Eclipse, you should be able to set a breakpoint, and you can then examine variables, etc. But you can evaluate random python scripts via the watch functionality.

Categories

Resources