Pycharm:只运行我的 Python 文件的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23441657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:58:56  来源:igfitidea点击:

Pycharm: run only part of my Python file

pythonpycharm

提问by FrankTheTank

Is it possible to run only a part of a program in PyCharm?

是否可以在 PyCharm 中只运行程序的一部分?

In other editors there is something like a cell which I can run, but I can't find such an option in PyCharm?

在其他编辑器中,我可以运行类似单元格的东西,但在 PyCharm 中找不到这样的选项?

If this function doesn't exist it would be a huge drawback for me... Because for my data analysis I very often only need to run the last few lines of my code.

如果这个函数不存在,那对我来说将是一个巨大的缺点......因为对于我的数据分析,我经常只需要运行我的代码的最后几行。

回答by PyTony

You can select a code snippet and use right click menu to choose the action "Execute Selection in console".

您可以选择一个代码片段并使用右键单击菜单选择“在控制台中执行选择”操作。

回答by Games Brainiac

You can set a breakpoint, and then just open the debug console. So, the first thing you need to turn on your debug console:

可以设置断点,然后直接打开调试控制台。因此,您需要打开调试控制台的第一件事:

enter image description here

在此处输入图片说明

After you've enabled, set a break-point to where you want it to:

启用后,将断点设置到您想要的位置:

enter image description here

在此处输入图片说明

After you're done setting the break-point:

设置断点后:

enter image description here

在此处输入图片说明

Once that has been completed:

一旦完成:

enter image description here

在此处输入图片说明

回答by Wisienkas

Pycharm shortcut for running "Selection" in the console is ALT + SHIFT + e

在控制台中运行“选择”的 Pycharm 快捷方式是 ALT + SHIFT + e

For this to work properly, you'll have to run everything this way.

为了使其正常工作,您必须以这种方式运行所有内容。

enter image description here

在此处输入图片说明

回答by Willian Adamczyk

I found out an easier way.

我发现了一个更简单的方法。

  • go to File -> Settings -> Keymap
  • Search for Execute Selection in Consoleand reassign it to a new shortcut, like Crl+Enter.
  • 转到文件 -> 设置 -> 键盘映射
  • 搜索Execute Selection in Console并将其重新分配给新的快捷方式,例如Crl+Enter.

This is the same shortcut to the same action in Spyder and R-Studio.

这是 Spyder 和 R-Studio 中相同操作的相同快捷方式。

回答by floatsd

  1. Go to File >> Settings >> Plugins and install the plugin PyCharm cell mode
  2. Go to File >> Settings >> Appearance & Behavior >> Keymap and assign your keyboard shortcuts for Run Celland Run Cell and go to next
  1. 转到文件>>设置>>插件并安装插件 PyCharm cell mode
  2. 转到文件>>设置>>外观和行为>>键盘映射并为Run Cell和分配键盘快捷键Run Cell and go to next

A cell is delimited by ##

单元格由 ## 分隔

Refhttps://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode

参考https://plugins.jetbrains.com/plugin/7858-pycharm-cell-mode