Jonathan Dobres

running the current python file in the vscode terminal

Suppose you’re like me, and you find Jupyter notebooks unwieldy and sluggish. Maybe, like me, you’re coming to Python from R, and what you really want is an equivalent of R’s source function. You just want a way to execute a plain .py file in a live Python session (preferably without printing every individual line to the terminal), and then keep that session open.

Well, good news! If you’re using Visual Studio Code, you can just add the following to your keybindings.json file (change shift+cmd+enter to whatever you’d like, of course). This executes the current file in the active terminal. Think of it as a Jupyter-style “Run All Cells” command, but for plaintext Python.

    "key": "shift+cmd+enter", 
    "command": "workbench.action.terminal.sendSequence",
    "args": {
        "text": "exec(open('${file}').read())\n"