ChatGPT解决这个技术问题 Extra ChatGPT

interactive shell debugging with pycharm

I am new to PyCharm. I have been using IDLE for a long time.

It is very convenient to use Python objects after script execution in IDLE. Is there any way to use script objects after its execution with interactive python shell using PyCharm?

For example, we have a 'test' project with one file 'test.py':

a = '123'
print a

after execution we can get the result:

123
Process finished with exit code 0

How can I use string 'a' with interactive shell?

Added after 8 years. My pycharm tutorial in russian: https://youtu.be/-RzKJlLXw54


c
ccpizza

Built-in python shell for the current debugging session

Set a breakpoint at the line of interest in your code (i.e. by clicking the gutter), and launch debug (right-click in the editor then pick Debug myfile.py...). When the breakpoint is reached, locate the Debug > Console tab, and then click the Show command line icon (see screenshot).

This will enable a python shell (notice the green >>> on the screenshot) where you can access all the variables in the current scope, and do everything you usually do in the Python shell.

In recent pycharm versions you get the full ipython interpreter instead of the plain python shell (if ipython is installed).

https://i.stack.imgur.com/mTHDA.jpg

The Evaluate expression window

As a more comfortable alternative, if you only need to inspect some variables, access members or call methods on an object in scope, once a breakpoint is reached, select an expression in the current scope, right-click -> Evaluate Expression (or use the hotkey shown in the menu under Run → Evaluate Expression...), edit as needed — you can type any python expression, with auto-completion available — and then press Enter (or click Evaluate) to inspect the result.

https://i.stack.imgur.com/ZWsxL.png

Mouse hover over variables

To see the value of a variable after you hit a breakpoint in debug mode, hover the mouse pointer over the variable (1-2 seconds) and the value will be shown in a tooltip.

https://i.stack.imgur.com/y1DHL.png

The hint will contain a ➕ icon — clicking it will open the inspector in a popup.

https://i.stack.imgur.com/YF9Fs.png

For the variables in scope the inspector is shown in the bottom panel under Debug > Debugger.

For pandas data frames you will see a View as DataFrame link in the variable inspector panel or in the hover popup — clicking the link will display the dataframe as a table in the Data View panel.

Update

In the more recent Pycharm versions (2019+) the interpreter icon now looks different:

https://i.stack.imgur.com/158Jd.png

Also in the unittest/pytest debugging UI the icon is placed first in the icon bar.


"Show command line" is a function which You even can bind to a keyboard shortcut.
Switching tabs from Debugger to Console can be done with Alt-Left or Alt-Right
j
jezrael

I found to previous answers from Piga-fetta, Games Brainiac and kobejohn useful, but not satisfying. So I here provide a third option:

Loading selected code into the console (my suggestion)

Use Shift + Alt + E to load the selected code or the line in which the cursor is placed into the console and immediately run it. This also have some disadvantages:

You have to select the whole file if you need to run the whole file.

The code keeps running even if it encounters an error.

But in return we get a feature that is better than IDLE (in my opinion): Being able to run your code one selection at a time.

Read about it here.

Using breakpoints and Evaluate Expression (Alt-F8) (suggested by Piga-fetta)

This is very useful in big application where we need to debug at certain locations in the code, but not so useful for interactive coding. So this is not what we want.

Using Tools --> Run Python Console (suggested by Games Brainiac and kobejohn)

This is want we want, but is is a bit cumbersome, especially if the the module we want to run is not in the root directory of the project.


G
Games Brainiac

You can simply use the Python Console inside both PyCharm 2 and PyCharm 3. And you can simply import since your project root is already added to your PYTHONPATH:

So let me demonstrate through some screen shots:

1. Making a console.py file in root directory

https://i.stack.imgur.com/4fYuN.png

2. Opening up Python Console inside PyCharm

https://i.stack.imgur.com/JSlv8.png

3. Import variable from console.py file

https://i.stack.imgur.com/fAS43.png

And there, you have imported your variable successfully.


s
strpeter

Leave command line open after executing

For anyone still having this problem: Go to the Run/Debug menu, choose Edit Configuration, check the box 'Show command line' this will enable you to enter parameters in the console at the >>> prompt and test your function.

https://i.stack.imgur.com/U0p1N.png

Global configuration

To make this change apply to all your .py files (as this check box only applies to the current file you're working on) go to: Edit configuration, in the pop up you will see a menu tree on the left, select Defaults, then Python, then check the 'Show command line' box, this will make it the default setting whenever you open a .py file, (this feature should really be on by default!)

https://i.stack.imgur.com/HUYt8.png


Instead of Show command line afterwards the option is now Run with Python console
C
Community

I found the best answer in: Interacting with program after execution

Quoting the answer below:

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

From output of python --help:

-i : inspect interactively after running script; forces a prompt even if stdin does not appear to be a terminal; also PYTHONINSPECT=x

To set interpreter option in PyCharm go to Run|Edit Configuration

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

I tried it and it works - simply use "-i" (without quotation marks) as "Interpreter options". Note that if you only change the Defaults in the Run/Debug Configurations, it may not take immediate effect for scripts you've already run before; you will need to edit the configurations of those scripts one by one.


At least in 4.5.4 PyCharm offers a "Show command line afterwards" option in the Rund/Debug Configurations dialog that I found much better than the -i option (arrow up for last command works)
P
Piga-fetta

Not mentioned above:

If you want to use a variable during execution, e.g. to when you set a breakpoint and then experiment with calling functions on objects in current scope, PyCharm has an 'Evaluate Expression (Alt-F8)' popup window.

In this window, you can call functions and see the output. Code completion also works. This window also has a "code fragment mode", I am just researching what it means - can you define temporary functions here?.

(I am using PyCharm 3.0.1 Community Edition)


C
Community

*update

From your update, I think this SO question provides at least one perfect answer.

Let me write it here a little more step by step than the answer I linked.

Tools --> Run Python Console

import your script import myscript (without .py) (if you want a short name, use import myscript as m

(you may need to run main() if you have an if __name__ == '__main__' block)

as in your question, a is available in myscript.a

if you want to, run the script again with myscript = reload(myscript)

*original

In PyCharm 3.0 you can do the following:

Tools --> Run Python Console

Tools --> Open Terminal (from which you can start python or do whatever)

Is that what you are looking for? If not, please try them and let me know how that is different from what you want.


a
arkore

A further alternative is to simply use the same command that Spyder uses to "interactively" run a script:

>>> runfile('myscript.py')

Then you can open the variable explorer for the interactive console and rerun the script by running the above command again. Very similar to the Spyder workflow. All the other above methods will leave you with an interactive console prompt but not the option to open a variable explorer so if you are looking for that kind of feature, try the above.


r
rovyko

In addition to the suggestion I made on Ramkins's answer, you can run the file directly with the console by right clicking inside the file and selecting Run File in Console.