Add keyboard shortcut to run .py script with current file as argument

671 Views Asked by At

Assuming I have a .py script in ~/Scripts/script.py, how can I run it with the currently opened file in PyCharm 2020.3 Professional?

What I simply need is to bind a keyboard shortcut to a python ~/Scripts/script.py <current_file> command and see the output in console.

Edit: I guess I need a PyCharm equivalent of https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner

1

There are 1 best solutions below

2
PythonSnek On

Simplest way would be to get AutoHotKey and use that, as it has a lot of functionality, and because I don't think you can set extra shortcuts in PyCharm, both versions. You could try this though in a separate file:

import os
os.system("path/to/script.py path/to/otherfile.py")

or if you need to give the second file as STDIN or input:

import os
os.system("path/to/script.py<path/to/otherfile.py)

Warning though, the second will turn your otherfile.py into STDIN and therefore it will be readable line by line with input() Extra
AHK Script for you:

#SingleInstance, force
; # is for windows key, ! is for Alt key, ^ is for control key, + is for Shift key
^+r:: ; Will use Ctrl+Shift+R
Run, python path/to/script.py path/to/otherfile.py
return