65

How can I run the current file in PyCharm? I would like a single hotkey that will execute the current file (whether normal file, scratch file, or scratch buffer) using the project default python interpreter. I would like to NOT have to create a custom run configuration, but just launch with the default Python configuration. Is such a thing possible?

5
  • 3
    Isn't that what Ctrl+Shift+F10 does?
    – Ma0
    May 5, 2017 at 14:19
  • 2
    YES! That is what I was looking for! If you write it as an answer I will accept it.
    – alvion
    May 7, 2017 at 13:20
  • I just answered myself and accepted my own answer. If you would like to post your own answer, I would gladly accept yours.
    – alvion
    May 8, 2017 at 22:02
  • I already did that yesterday but no worries.
    – Ma0
    May 9, 2017 at 8:10
  • Oh yeah, sorry I missed that. If you would be willing to amend your answer to mention "Run in Context" so that users on other OSes or with different key bindings can make use of it, then I'll change the correct answer to yours. Thanks for the help!
    – alvion
    May 11, 2017 at 14:38

12 Answers 12

48

As it turns out, the action I was seeking is "Run context configuration" (or "Debug context configuration" for debugging). The default key binding on Windows is ctrl+shift+f10, or ctrl+option+R on Mac, as Ev. Kounis pointed out, although you can bind it to any key you like.

These settings can be found under the "Other" section in File->Settings->Keymap. The easiest way to find them is to simply use the search box.

33

Just right click on the tab of your file and click run "file name" or you can press CTRL+shift+F10

Screenshot

1
  • 1
    I was looking for that method in the whole internet. I just forgot that we can right click on tabs in pycharm. May 26, 2019 at 12:14
13

The key combination you are looking for is Ctrl + Shift + F10. This will run the current script with current being the one displayed in the viewer.

1
  • Alt+Shift+F10 in PyCharm 2020.3. Feb 11, 2021 at 3:55
8

There are several alternatives:

  1. Run context configuration from editor shortcut:

Mac: R

Windows/Linux: Ctrl + Shift + F10

  1. You can right click on the file, and then run Run 'filename'.

enter image description here

  1. You can also run the shortcut to Find Action and then type run:

Mac: A

Windows/Linux: Ctrl + Shift + A

enter image description here

  1. If the current file was just run, you can simply use the Run shortcut:

Mac: R

Windows/Linux: Shift + F10


References:

PyCharm Shorcuts for Mac OS X

PyCharm Shorcuts for Windows and Linux

3

Alt+Shift+F10 and then select the script you want to run.

After that Shift+F10 will run the last script that has been run.

2

As of PyCharm 2022.2 there's a built-in "Current File" configuration.

1

Keyboard shortcuts can be different on some machines. So you can just click right key on mouse and then "Run "(also you can select part of code and do the same)

1

File->Settings->Keymap->Run->Run and see your current keymap

7
  • This will give me the option to run the current file as one of the items in the list, but won't do it immediately.
    – alvion
    May 7, 2017 at 13:05
  • you're not specifying what keymap you have, neither your OS. You can't ask for shortcuts for an IDE without specifying any of these. So yes Ctrl+Shift+F10 works for you, but won't work for other cases/people. That's why I pointed you to see/set your own shortcut. May 8, 2017 at 9:00
  • That's true of course. The way key bindings work in PyCharm is that you associate a key with an action, so if there is an existing action that I could bind the key to, that would be an acceptable answer. The reason I didn't ask for an action specifically is that I thought that the answer may involve a macro, a plugin, etc. As it turns out that is not the case, and I will show in my own answer, based on the shortcut that Ev. Kounis suggested.
    – alvion
    May 8, 2017 at 21:59
  • lol sure ok mark your answer as correct. then someone who runs linux/macos or pycharm has imported other keymappings can mark your answer as not useful because ctrl+shift+f10 will not work on their machine ^^ May 9, 2017 at 13:24
  • The correct answer is to use "Run in Context", which I stated clearly in my answer. I only mentioned "ctrl+shift+f10" in passing as an example. Your answer is utterly and obviously wrong, so I don't know what you're complaining about.
    – alvion
    May 11, 2017 at 14:36
1

Basically, if you just need to run the current .py file in PyCharm. Right-click inside the file, and you can click the "Run file.py" button, and it also tells you the shortcut which on Mac is Control + Shift + R.

1

To Run/Execute current python file in Pycharm use following keys in windows machine

Shift+Ctrl+F10

OR

for selected code (specific line that are selected/marked)

Shift+Alt+E

1

Running file with Ctrl+Shift+F10 or via Run menu has some disadvantages. For example, you cannot use the custom redirect input file.

I have created a project it is that will actually allow you to use currently opened py file when you press run / debug in a standard way. My project will remain actual at least until jetbrains fix non working macros in configurations. See bug report.

-1

When intalling on Pycharm select : Add launchers dir to the PATH: Allows running this PyCharm instance from the Console without specifying the path to it.

:-)

1
  • 1
    Welcome to SO! When you are about to answer an old question (this one is over 3 years old) that already has an accepted answer (that is the case here) please ask yourself: Do I really have a substantial improvement to offer? If not, consider refraining from answering.
    – Timus
    Oct 28, 2020 at 20:01

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.