How to insert code in an interactive python console througth Visual Studio Code

After run your code, to edit or add new instructions is not neccesary run all your code again.
A good idea is to run a partial or section code. To do it, you must run your code as interactive mode.

You have 3 ways to do it with vscode:

1.- Task
You can create a task to run python in interactive mode.
Press Ctrl+Shift+P and type: Configure Task
Paste the following code:

{
"version": "2.0.0",
"tasks": [
{
"label": "Run File",
"command": "python3 -i ${file}",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"focus": true
}
}

This task is named "Run File" and execute python3 in interactive mode. Focus options allows focus keyboard on the terminal.
To run the task press Ctrl+Shift+B

2.- Code Runner extension.
To install it, press Ctrl+P and type: ext install formulahendry.code-runner
Now, configure your settings pressing Ctrl+, and add the following code:

"code-runner.runInTerminal": true,
"code-runner.executorMap":
{
"python": "/usr/bin/python3 -i"
}

By default, code runner execute itself in output tab (it not allows data input)
To run code runner just press Ctrl+Alt+N

3.- If you use python extension, you can execute your code by default in terminal, with rigth click buttom.







To run it in interactive mode, you must edit your settings with Ctrl+, and add this code:

"python.terminal.launchArgs": ["-i"],

To run it in terminal I can't found a shortcut, but is it the only way that I found to add code to interactive mode without execute all code again (if you don't like copy the code and paste it in the terminal to execute it).
To do it, just press Shift+Enter on your selected code.

Enjoy it!

Comments

Popular posts from this blog

How to fix Android when developer options are not available for this user

from: can't read /var/mail/ in Python scripts

Fix "Set scan parameters failed: Input/output error" on Ubuntu 16.04