简体   繁体   English

使用 pycharm 进行交互式 shell 调试

[英]interactive shell debugging with pycharm

I am new to PyCharm.我是 PyCharm 的新手。 I have been using IDLE for a long time.我已经使用 IDLE 很长时间了。

It is very convenient to use Python objects after script execution in IDLE.在 IDLE 中执行脚本后使用 Python 对象非常方便。 Is there any way to use script objects after its execution with interactive python shell using PyCharm?在使用 PyCharm 通过交互式 python shell 执行脚本对象后,有什么方法可以使用它吗?

For example, we have a 'test' project with one file 'test.py':例如,我们有一个包含一个文件“test.py”的“test”项目:

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?如何在交互式 shell 中使用字符串 'a'?

Added after 8 years. 8年后添加。 My pycharm tutorial in russian: https://youtu.be/-RzKJlLXw54我的 pycharm 俄文教程: https ://youtu.be/-RzKJlLXw54

Built-in python shell for the current debugging session当前调试会话的内置 python shell

  1. Set a breakpoint at the line of interest in your code (ie by clicking the gutter), and launch debug (right-click in the editor then pick Debug myfile.py... ).在代码中感兴趣的行上设置断点(即通过单击装订线),然后启动调试(在编辑器中右键单击,然后选择Debug myfile.py... )。
  2. When the breakpoint is reached, locate the Debug > Console tab, and then click the Show command line icon (see screenshot).到达断点后,找到Debug > Console选项卡,然后单击Show command line图标(参见屏幕截图)。

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.这将启用一个 python shell(注意屏幕截图上的绿色>>> ),您可以在其中访问当前范围内的所有变量,并执行您通常在 Python shell 中执行的所有操作。

In recent pycharm versions you get the full ipython interpreter instead of the plain python shell (if ipython is installed).在最近的 pycharm 版本中,您可以获得完整的 ipython 解释器而不是普通的 python shell(如果安装了 ipython)。

为已调试的应用程序启用 Python shell

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

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

在此处输入图片说明

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 .对于范围内的变量,检查器显示在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 .对于pandas的数据帧,你会看到一个视图中的变量检查面板或悬停弹出数据框链接-点击该链接将显示数据帧的数据视图面板表

Update更新

In the more recent Pycharm versions (2019+) the interpreter icon now looks different:在最近的 Pycharm 版本(2019+)中,解释器图标现在看起来不同了:

pycharm 解释器图标

Also in the unittest/pytest debugging UI the icon is placed first in the icon bar.同样在 unittest/pytest 调试 UI 中,图标首先放置在图标栏中。

I found to previous answers from Piga-fetta, Games Brainiac and kobejohn useful, but not satisfying .我发现之前 Piga-fetta、Games Brainiac 和 kobejohn 的答案很有用,但并不令人满意 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.使用Shift + Alt + E将选定的代码或光标所在的行加载到控制台中并立即运行它。 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.但作为回报,我们得到了一个比 IDLE 更好的特性(在我看来):能够一次一个选择地运行你的代码。

Read about it here . 在这里阅读。

Using breakpoints and Evaluate Expression ( Alt - F8 ) (suggested by Piga-fetta)使用断点和评估表达式( Alt - F8 (由 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)使用工具 --> 运行 Python 控制台(由 Games Brainiac 和 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.这是我们想要的,但是有点麻烦,特别是如果我们要运行的模块不在项目的根目录下。

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 :您可以简单地在 PyCharm 2 和 PyCharm 3 中使用 Python 控制台。并且您可以简单地导入,因为您的项目根目录已经添加到您的PYTHONPATH

So let me demonstrate through some screen shots:那么让我通过一些屏幕截图来演示:

1. Making a console.py file in root directory 1.在根目录下制作console.py文件

在此处输入图片说明

2. Opening up Python Console inside PyCharm 2. 在 PyCharm 中打开 Python 控制台

在此处输入图片说明

3. Import variable from console.py file 3.从console.py文件导入变量

在此处输入图片说明

And there, you have imported your variable successfully.在那里,您已成功导入您的变量。

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.对于仍然有此问题的任何人:转到“运行/调试”菜单,选择“编辑配置”,选中“显示命令行”框,这将使您能够在 >>> 提示符下在控制台中输入参数并测试您的功能。

仅更改当前文件的设置

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!)要将此更改应用于所有 .py 文件(因为此复选框仅适用于您正在处理的当前文件)转到:编辑配置,在弹出窗口中您将看到左侧的菜单树,选择默认值,然后是 Python,然后选中“显示命令行”框,这将使其成为每次打开 .py 文件时的默认设置(默认情况下,此功能应该真正启用!)

更改所有 Python 文件的设置

I found the best answer in: Interacting with program after execution我在以下文章中找到了最佳答案: 执行后与程序交互

Quoting the answer below:引用下面的答案:

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

From output of python --help:从 python --help 的输出:

-i : inspect interactively after running script; -i : 运行脚本后交互检查; forces a prompt even if stdin does not appear to be a terminal;即使 stdin 似乎不是终端,也会强制提示; also PYTHONINSPECT=x还有 PYTHONINSPECT=x

To set interpreter option in PyCharm go to Run|Edit Configuration要在 PyCharm 中设置解释器选项,请转到运行|编辑配置

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

I tried it and it works - simply use "-i" (without quotation marks) as "Interpreter options".我试过了,它有效 - 只需使用“-i”(不带引号)作为“解释器选项”。 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.您需要一一编辑这些脚本的配置。

Not mentioned above:上面没有提到:

If you want to use a variable during execution, eg 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.如果你想执行过程中使用一个变量,例如当你设置一个断点然后在当前范围内的对象上尝试调用函数时,PyCharm 有一个“评估表达式(Alt-F8) ”弹出窗口。

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) (我使用的是 PyCharm 3.0.1 社区版)

*update *更新

From your update, I think this SO question provides at least one perfect answer .从您的更新来看,我认为这个 SO 问题至少提供了一个完美的答案

Let me write it here a little more step by step than the answer I linked.让我在这里一步一步地写出来,而不是我链接的答案。

  • Tools --> Run Python Console工具 --> 运行 Python 控制台
  • import your script import myscript (without .py) (if you want a short name, use import myscript as m导入您的脚本import myscript (不带 .py)(如果您想要一个短名称,请使用import myscript as m
  • (you may need to run main() if you have an if __name__ == '__main__' block) (如果你有一个if __name__ == '__main__'块,你可能需要运行main()
  • as in your question, a is available in myscript.a与您的问题一样, amyscript.a可用
  • if you want to, run the script again with myscript = reload(myscript)如果需要,请使用myscript = reload(myscript)再次运行脚本

*original *原来的

In PyCharm 3.0 you can do the following:在 PyCharm 3.0 中,您可以执行以下操作:

  • Tools --> Run Python Console工具 --> 运行 Python 控制台
  • Tools --> Open Terminal (from which you can start python or do whatever)工具 --> 打开终端(您可以从中启动 python 或执行任何操作)

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 further alternative is to simply use the same command that Spyder uses to "interactively" run a script:另一种选择是简单地使用 Spyder 用来“交互式”运行脚本的相同命令:

>>> 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.非常类似于 Spyder 工作流程。 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.上述所有其他方法都会为您提供交互式控制台提示,但不会提供打开变量资源管理器的选项,因此如果您正在寻找那种功能,请尝试上述方法。

除了我对Ramkins 的回答提出的建议之外,您还可以通过右键单击文件内部并选择Run File in Console直接使用控制台运行该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM