How to setup Code Runner in Visual Studio Code for Python

Here's another alternative answer, I think more accurate.
Add following settings to your vscode user settings file:

"code-runner.executorMap": {
    "python": "$pythonPath -u $fullFileName",
},

Check out this reference for some useful variables: Variables Reference


You can provide input by telling code runner to use the terminal. To do this, there is a setting called code-runner.runInTerminal, set to false by default, that you can set to true.

There is one more thing that you should watch out for if you are using a windows command line for the terminal like CMD or PowerShell. If your project directory has spaces in it (e.g. C:\Example Test) you will get an error. To fix this, you need to add escaped quotation marks (\") around the directory path variables (normally $dir or $workspaceRoot) found under the setting code-runner.executorMap and code-runner.executorMapByFileExtension in the user settings.


The main problem here is that the output window that the code runner extension uses by default is read only. If you use the terminal instead, your program will be able to accept input as normal.

You can configure Code Runner to use the integrated terminal instead of the output window by setting the code-runner.runInTerminal setting to true (the default is false). In the settings.json file it should look like: "code-runner.runInTerminal": true

If you want to use the GUI instead the setting should look like this once set to true. Run In Terminal setting using the gui interface

If you are using a virtual environment instead of the system python install, you will also need to configure a second setting for it to work properly with installed modules. The code-runner.executorMap setting will configure what code runner actually does once you press run or use the Ctrl + Alt + N shortcut. By default it seems to simply invoke the python interpreter added to the PATH.

If you change the setting in the settings.json file to:

"code-runner.executorMap": {
    "python": "$pythonPath -u $fullFileName"
}

then Code Runner will use whatever value is in the pythonPath variable instead. You can set this using the Python: Select Interpreter command from the command palette (Ctrl + Shift + P). This way you can select the interpreter in your virtual environment and use that instead of the one attached to the PATH by default.

The two settings above should allow you to A) Enter input inside of the integrated terminal and B) Select which python interpreter code-runner should execute easily using existing commands.