ChatGPT解决这个技术问题 Extra ChatGPT

Pycharm: run only part of my Python file

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

In other editors there is something like a cell which I can run, but I can't find such an option in 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.

You can use Jupyter notebook inside pyCharm. Just select new Jupyter notebook
Is it possible to run only a part of a program in PyCharm? Yes. You can refer to the following website: enter link description here

K
Kampai

I found out an easier way.

go to File -> Settings -> Keymap

Search for Execute Selection in Console and reassign it to a new shortcut, like Crl + Enter.

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


Very glad you added this answer! I'm sure all RStudio and Spyder users will appreciate this
Is there a way to do this without having to select the code section everytime ? For example in Spyder, we can write "#%%" at the beginning and end of the code section and we can execute it in console with Ctrl+Enter without having to select it.
@kanmani have a look at plugins.jetbrains.com/plugin/7858-pycharm-cell-mode it does what you ask for
Notice: despite the name "Execute Selection in Console", pointing the cursor to anywhere in the line and using this action will execute current line.
Is there a shortcut for running a selection in the terminal?
W
Wisienkas

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

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

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


P
PyTony

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


Unfortunately this doesn't work, since all the variables which are defined before my code snippet aren't in the workspace anymore.
@FrankTheTank You need to highlight all the appropriate code, including any imports. It's basically a copy-paste shortcut.
G
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:

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

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

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

After you're done setting the break-point:

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

Once that has been completed:

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


f
floatsd

Go to File >> Settings >> Plugins and install the plugin PyCharm cell mode Go to File >> Settings >> Appearance & Behavior >> Keymap and assign your keyboard shortcuts for Run Cell and Run Cell and go to next

A cell is delimited by ##

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


A
Amir Fo

There is an option that you can run jupyter notebook file and its cells in pycharm. For more information visit Run and debug Jupyter notebook code cells in pycharm.