Running a single line of code in PyCharm

Question:

I’m coming from RStudio, so forgive my naievete.

In RStudio it’s really nice to be able to run lines by simply pressing Ctrl+Enter on the line. This sends the line of code directly to the console, which makes building functions really easy because you can work through each line to check for problems. However, in Pycharm this doesn’t appear to be a feature; instead, you select with a mouse and run code (https://www.jetbrains.com/help/pycharm/loading-code-from-editor-into-console.html).

This seems like a cumbersome way to write code. Is there a way to run a single like of code like in RStudio?

Asked By: Vedda

||

Answers:

ALT+SHIFT+E will execute in console.

CTRL+SHIFT+A will let you search for hotkeys.

Answered By: krflol

You can also look into "Evaluate Expression" option, which let’s you run code in the current scope while debugging, it’s usefull for lookups and modification in runtime.

Here is link to IDEA, but it’s the same with PyCharm:
(https://www.jetbrains.com/help/idea/evaluating-expressions.html)

Answered By: daboycharlie

It is also possible to change the key combination to your liking.

For example, if you used to work with ctrl + enter in R, you can change it in:

File -> Settings -> Keymap - Other -> Execute selection in Python Console

Answered By: StefanK

Currently the best option:

  1. First download the free plugin Python Smart Execute

  2. Use the key combination OPTION + SHIFT + A

  3. If preferred change the key combination to COMMAND + ENTER

Despite not skipping blank lines, it pretty much resembles the way you can execute code in RStudio

Answered By: peter

I know that question is about PyCharm. But for people who came here from Visual Studio Code world and wonder is there such feature. It is simple as SHIFT+ENTER.

Answered By: Mikhail Kashkin

In PyCharm 2022.2 (not sure about other versions):

File -> Settings -> Keymap -> Plugins -> Python Community Edition -> Execute Selection in Python Console -> Change this to Ctrl + ENTER -> Accept prompt’s suggestion to clear this shortcut for other commands -> Apply

Answered By: Dmitry Ishutin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.