使用 pycharm 进行交互式 shell 调试 [英] interactive shell debugging with pycharm

查看:105
本文介绍了使用 pycharm 进行交互式 shell 调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PyCharm 的新手.我已经使用 IDLE 很长时间了.

在 IDLE 中执行脚本后使用 Python 对象非常方便.使用 PyCharm 通过交互式 python shell 执行脚本对象后,有什么方法可以使用它吗?

例如,我们有一个包含一个文件test.py"的test"项目:

a = '123'打印一个

执行后我们可以得到结果:

123进程已完成,退出代码为 0

如何在交互式 shell 中使用字符串 'a'?

解决方案

为当前调试会话内置 python shell

  1. 在代码中感兴趣的行设置断点(即通过单击装订线),然后启动调试(在编辑器中右键单击,然后选择 Debug myfile.py...).
  2. 到达断点时,定位Debug >控制台标签,然后点击显示命令行图标(见截图).

这将启用一个 python shell(注意屏幕截图上的绿色 >>>),您可以在其中访问当前作用域中的所有变量,并执行您通常在Python 外壳.

<块引用>

在最近的 pycharm 版本中,您将获得完整的 ipython 解释器,而不是普通的 python shell(如果安装了 ipython).

评估表达式窗口

作为一个更舒服的选择,如果你只需要在作用域内的对象上检查一些变量、访问成员或调用方法,一旦到达断点,在当前作用域中选择一个表达式,右键单击 ->Evaluate Expression(或使用Run下菜单中显示的热键→Evaluate Expression...),根据需要进行编辑——您可以输入<强>任何 python 表达式,自动完成可用 - 然后按Enter(或点击评估em>) 检查结果.

支持多行和多行表达式:要获得检查对话框的多行版本,请单击代码片段模式或在编辑器中选择多行代码,然后对表达式求值.您可以为现有变量赋值(不能定义新变量),并运行整个代码块.

鼠标悬停在变量上

要在调试模式下遇到断点后查看变量的值,请将鼠标指针悬停在变量上(1-2 秒),该值将显示在工具提示中.

提示将包含一个 图标 - 单击它会在弹出窗口中打开检查器.

对于范围内的变量,检查器显示在 Debug 下的底部面板中 >调试器.

对于

同样在 unittest/pytest 调试 UI 中,图标首先放置在图标栏中.

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?

解决方案

Built-in python shell for the current debugging session

  1. 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...).
  2. 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).

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 RunEvaluate 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.

Multiple and multiline expressions are supported: to get a multiline version of the Inspect dialog click the Code fragment mode or select more than one line of code in the editor and then evaluate the expression. You can assign values to existing variables (new ones cannot be defined), and run entire chunks of code.

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.

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

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:

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

这篇关于使用 pycharm 进行交互式 shell 调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆