简体   繁体   English

Visual Studio代码交互式python控制台

[英]Visual studio code interactive python console

I'm using visual studio code with DonJayamanne python extension. 我正在使用带有DonJayamanne python扩展的visual studio代码。 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. 它运行正常,但我希望有一个像Matlab中的交互式会话,在代码执行后,每个定义和计算结果都保留在控制台中并可访问。

For example after running this code: 例如,运行此代码后:

a = 1

the python session is terminated and I cannot type in the console something like: python会话终止,我不能在控制台中键入如下内容:

b = a + 1
print(b)

I'm aware that the python session can stay alive with a "-i" flag. 我知道python会话可以使用“-i”标志保持活动状态。 But this simply doesn't work. 但这根本行不通。

Also every time I run a code file, a new python process is spawned. 每次运行代码文件时,都会生成一个新的python进程。 Is there a way to run consecutive runs in just one console? 有没有办法在一个控制台中运行连续运行? Again like Matlab? 再次像Matlab一样?

This sounds really essential and trivial to me. 这对我来说听起来非常重要和微不足道。 Am I missing something big here that I can't find a solution for this? 我错过了一些我无法找到解决方案的东西吗?

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 . 启动终端窗口并输入python
    Every statement executed in the REPL is within the same session. 在REPL中执行的每个语句都在同一个会话中。

  2. Next version will add support for Jupyter. 下一个版本将添加对Jupyter的支持。
    Please have a look here for some samples of what is yet to come: 请看这里有一些未来的样本:

I add those lines into user setting file, then it works. 我将这些行添加到用户设置文件中,然后它可以工作。 select some lines of python code,then "run selected code in python terminal" 选择一些python代码行,然后“在python终端中运行选定的代码”

solution 1: will start ipyhon terminal 解决方案1:将启动ipyhon终端

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

Solution 2: will start a terminal like "python -i" 解决方案2:将启动像“python -i”这样的终端

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

so 所以

以下行将解决您的问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM