24

I have installed Visual Studio Code 1.23.1 and added extensions - Python, Code Runner.

With Code Runner, now I can see the Run Code symbol (triangle) and on highlighting it, I see the shortcut Ctrl + Alt + N. But when I try to use it to run the code that asks for user input, I can't find a way to provide the input. When I try to enter user input, I get error message "Cannot edit in read-only editor". I think this is because I am missing some configuration part for Code Runner like setting up PATH or some other Workspace settings.

Question: Please assist me in identifying what all configuration will I need to do and how?

I did select "Add Python 3.6 to PATH" while installing Python. I have attached screenshots for reference:

ExecutionError ExecutionError

Note: Even now when I right click and select "Run Python File in Terminal" for the same program, I can enter user input fine and get the expected output. ExecutionWorking

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
300
  • 965
  • 1
  • 14
  • 53
  • Does this answer your question? [Visual Studio Code: Take Input From User](https://stackoverflow.com/questions/36964949/visual-studio-code-take-input-from-user) – Second Person Shooter Apr 15 '21 at 02:49

3 Answers3

50

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

Lukewcn
  • 678
  • 1
  • 6
  • 8
  • 1
    Mate where can I get a list of all those $variables ? – Rishav Aug 25 '19 at 11:16
  • 1
    You saved my day! I was going crazy here! – Mysterio Nov 23 '19 at 15:11
  • 3
    for the reference of future comers: those variables in the link provided by the answer is not valid(well some of them, but not all of them), the supported ones are listed in code runner extension explanations. – muyustan Apr 12 '20 at 19:22
  • $pythonpath is not recognized in the terminal VSCode. Is this a predefined variable or your construct to put the python path here. – Timo Nov 06 '20 at 19:12
  • Can someone give a start point and an example for these variables in the link (Var Reference) used in launch.json. – Timo Nov 06 '20 at 19:14
29

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.

Zack Jorquera
  • 554
  • 7
  • 13
  • Thank you Zack. That worked! I had lost this question long time before and didn't expect that it'll get answered. I greatly appreciate your answer. Email notification for this question brought me back here and found your answer. – 300 Aug 07 '18 at 21:01
  • 4
    Where is the `code-runner.runInTerminal`? The plugin docs describe all the features of `code-runner.whatever` but does not say how to access it. – Mote Zart Jul 15 '19 at 04:28
  • If you enter ctrl-, (control + comma) and bring up the settings, you can then search for "code runner terminal" - this should give you two options. The first is a check-box for "Whether to run code in Integrated Terminal" Check the box and you should be good to go. – clay Jan 29 '23 at 16:34
13

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.

Jordan Cottle
  • 154
  • 1
  • 6