Visual studio code interactive python console

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39828744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:46:35  来源:igfitidea点击:

Visual studio code interactive python console

pythonipythonvisual-studio-code

提问by user1

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.

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:

For example after running this code:

a = 1

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

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.

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?

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?

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

回答by Don

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

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:

  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:

回答by Chaos2020

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

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

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"

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

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

回答by nirualapm

The following line will solve your problem.

The following line will solve your problem.

 "python.terminal.launchArgs": ["-c","\"from IPython import embed; embed()\""]