Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual studio code interactive python console

I'm using visual studio code with DonJayamanne python extension. It's working fine but I want to have an interactive session just like the one in Matlab, where after code execution every definition and computational result remains and accessible in the console.

For example after running this code:

a = 1

the python session is terminated and I cannot type in the console something like:

b = a + 1
print(b)

I'm aware that the python session can stay alive with a "-i" flag. But this simply doesn't work.

Also every time I run a code file, a new python process is spawned. Is there a way to run consecutive runs in just one console? Again like Matlab?

This sounds really essential and trivial to me. Am I missing something big here that I can't find a solution for this?

like image 308
user1 Avatar asked Oct 03 '16 09:10

user1


People also ask

How do I open interactive console in VSCode?

To open the terminal: Use the Ctrl+` keyboard shortcut with the backtick character. Use the View > Terminal menu command. From the Command Palette (Ctrl+Shift+P), use the View: Toggle Terminal command.

How do I create an interactive code in Python?

On Windows, bring up the command prompt and type "py", or start an interactive Python session by selecting "Python (command line)", "IDLE", or similar program from the task bar / app menu. IDLE is a GUI which includes both an interactive mode and options to edit and run files.

How do I enable interactive mode in Python?

A widely used way to run Python code is through an interactive session. To start a Python interactive session, just open a command-line or terminal and then type in python , or python3 depending on your Python installation, and then hit Enter .


3 Answers

I'm the author of the extension. There are two options:

  1. Use the integrated terminal window (I guess you already knew this)
    Launch the terminal window and type in python.
    Every statement executed in the REPL is within the same session.

  2. Next version will add support for Jupyter.
    Please have a look here for some samples of what is yet to come:

    • #303
    • Screen samples and more
like image 90
Don Avatar answered Oct 06 '22 10:10

Don


I added the following lines into user setting file, then it works. Select some lines of python code, then right-click and select Run selected code in python terminal

Solution 1: Will start iPython terminal

   "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
   "terminal.integrated.shellArgs.windows": ["/K ipython"],

Solution 2: Will start a terminal like "python -i"

   "python.terminal.launchArgs": ["-i"],
like image 21
Chaos2020 Avatar answered Oct 06 '22 12:10

Chaos2020


The following line will solve your problem.

 "python.terminal.launchArgs": ["-c","\"from IPython import embed; embed()\""]
like image 39
nirualapm Avatar answered Oct 06 '22 10:10

nirualapm