简体   繁体   English

Visual Studio Code - python 控制台

[英]Visual Studio Code - python console

I'm using visual studio code with standard python extension, my issue is that when I run the code the python interpreter instantly closes right after and I only see the output which means that if I create some data structure I have to create it every single time.我正在使用带有标准 python 扩展的 Visual Studio 代码,我的问题是当我运行代码时,python 解释器立即关闭,我只看到输出,这意味着如果我创建一些数据结构,我必须每一个都创建它时间。 Is it possible to leave the console open after running the code and maybe running multiple files in the same python interpreter instance?是否可以在运行代码后保持控制台打开并且可能在同一个 python 解释器实例中运行多个文件?

When you run a program, it runs until it ends.当你运行一个程序时,它会一直运行到结束。 Then it closes.然后它关闭。 If you want it to stay live longer, you can make a program which does not stop until told so, eg如果你想让它活得更久,你可以制作一个直到被告知才停止的程序,例如

while True:
    something = raw_input('Write something: ')
    print('You wrote: %s' % something)
    if something == 'bye':
        print 'bye.'
        break

This will run until user writes "bye".这将一直运行,直到用户写下“再见”。

I used to use spyder which is entirely doing what you want (probably like PyCharm)... Then I briefly tried VS Code, and it is quite easy to make it behave that way too.我曾经使用 spyder,它完全可以做你想做的事(可能就像 PyCharm)……然后我简单地尝试了 VS Code,而且很容易让它表现得那样。

First make sure you have an integrated terminal open (or do Ctrl+`, or View > Integrated Terminal), then in that terminal launch ipython .首先确保您打开了一个集成终端(或执行 Ctrl+`,或查看 > 集成终端),然后在该终端中启动ipython Now, when you use the commands from Don Jayamanne's Python extension (Ctrl+Shift+P to oppen commands palette):现在,当您使用 Don Jayamanne 的 Python 扩展中的命令时(Ctrl+Shift+P 打开命令面板):

  • "Run Python File in terminal" “在终端中运行 Python 文件”
  • "Run select/line in Terminal" “在终端中运行选择/行”

It will run the line inside the ipython console directly.它将直接在 ipython 控制台内运行该行。 So running the entire fail will call python module.py inside ipython, and thus fails .因此,运行整个失败将在 ipython 中调用python module.py ,从而失败

So to make this work simply create settings to map which command is executed when "Run select/line in terminal":因此,要完成这项工作,只需创建设置来映射“在终端中运行选择/行”时执行的命令:

  • Open Language specific settings (Shift+Ctrl+P, search "Configure Language specific Settings...")打开语言特定设置(Shift+Ctrl+P,搜索“配置语言特定设置...”)
  • Pick up Python拿起 Python
  • Now I would suggest to make change only in your workspace settings (top-right tab) to keep default behaviour in other cases so add in WORKSPACE SETTINGS :现在我建议仅在您的工作区设置(右上角选项卡)中进行更改以在其他情况下保持默认行为,因此添加WORKSPACE SETTINGS

(keep in mind it is just a simple/stupid workaround) (请记住,这只是一个简单/愚蠢的解决方法)

{
    "python.pythonPath": "run"
}

Now when runing whole file, it will use ipython function run within the ipython terminal that we launched, thus keeping all your workspace variables .现在,当运行整个文件时,它将使用 ipython 函数在我们启动的 ipython 终端中run ,从而保留所有工作区变量 Also, if you run some line of code with "Run Select/Line in Terminal", the ipython session of that terminal keep all variables.此外,如果您使用“在终端中运行选择/行”运行某行代码,则该终端的 ipython 会话会保留所有变量。

This allows live debugging, without actually going in the debug mode.这允许实时调试,而无需实际进入调试模式。

I'm quite late to this conversation, but a workaround I use is to put a pass statement at the end of my file, then add a breakpoint to it.我对这次谈话已经很晚了,但我使用的一种解决方法是在我的文件末尾放置一个 pass 语句,然后在其中添加一个断点。 I then run it in the debugger and can access all of the variables etc.然后我在调试器中运行它并可以访问所有变量等。

This allows most of the functionality that I used to use in the PyCharm python terminal, such as exploring data structures, checking out methods, etc. Just remember that, if you want to make a multi-line statement (eg. for a loop), you need to use Shift-Enter to go to the next line otherwise it'll try to evaluate it immediately.这允许我过去在 PyCharm python 终端中使用的大部分功能,例如探索数据结构、检查方法等。请记住,如果您想创建多行语句(例​​如,for 循环) ,您需要使用 Shift-Enter 转到下一行,否则它会立即尝试对其进行评估。

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

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