Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。

我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!

我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。

是否有一种方法可以在Visual Studio code中执行Python代码?


当前回答

按F5以调试模式运行 按Ctrl+F5运行调试,忽略断点。


按原样运行当前的python文件默认情况下没有关联的键绑定,但你可以通过以下方式设置:

Ctrl + Shift + P 输入“在终端中运行Python文件” 将鼠标悬停在上面并单击⚙️图标 双击“按键绑定” 设置您想要的快捷方式

其他回答

您不再需要任何额外的扩展。您可以简单地将调试器的输出切换到集成终端。

Ctrl+Shift+D,然后从顶部的下拉菜单中选择集成终端/控制台。

简单直接的Python扩展可以节省时间和精力。 检测、调试、代码完成是安装完成后可用的特性。在此之后,要运行代码,需要配置正确的Python安装路径以运行代码。一般设置可在“用户作用域”和“工作空间”中为Python语言-“Python”配置。pythonPath环境”:“c: / python27 / python.exe” 通过以上步骤,至少可以执行基本的Python程序。

在windows vs code上运行python3:

从官方网站下载python解释器 安装vs code的python包。这可以直接从vscode的扩展管理器中安装 通过运行以下命令,验证您的python3已经安装: Py -3—版本 在vscode的终端上使用以下命令运行脚本: Py -3 main.py

要了解更多信息,请前往这里了解详细的安装过程。

为了扩展vlad2135的答案(阅读他的第一个);这就是如何在Visual Studio Code中使用Don Jayamanne的Python扩展来设置Python调试(这是目前Python的一个功能非常全面的IDE,并且可以说是Visual Studio Code中最好的语言扩展之一,我认为)。

基本上,当你点击齿轮图标时,它会创建一个启动。Json文件在你的工作空间的。vscode目录。您也可以自己做这个,但是让Visual Studio Code来做这些繁重的工作可能会更简单。下面是一个示例文件:

生成后,您会注意到一些很酷的东西。它自动创建了一堆配置(我的大部分配置都被切断了;只要滚动就能全部看到),针对不同的库或环境(比如Django)有不同的设置和额外的功能。

你最终使用最多的可能是Python;这是一个简单的(在我的例子中是C)Python调试器,最容易使用设置。

我将对这个JSON属性做一个简短的介绍,因为其他的JSON属性使用几乎相同的配置,只是有不同的解释器路径和一两个不同的其他特性。

name: The name of the configuration. A useful example of why you would change it is if you have two Python configurations which use the same type of config, but different arguments. It's what shows up in the box you see on the top left (my box says "python" since I'm using the default Python configuration). type: Interpreter type. You generally don't want to change this one. request: How you want to run your code, and you generally don't want to change this one either. Default value is "launch", but changing it to "attach" allows the debugger to attach to an already running Python process. Instead of changing it, add a configuration of type attach and use that. stopOnEntry: Python debuggers like to have an invisible break-point when you start the program so you can see the entry-point file and where your first line of active code is. It drives some C#/Java programmers like me insane. false if you don't want it, true otherwise. pythonPath: The path to your install of Python. The default value gets the extension level default in the user/workspace settings. Change it here if you want to have different Pythons for different debug processes. Change it in workspace settings if you want to change it for all debug processes set to the default configuration in a project. Change it in user setting to change where the extension finds Pythons across all projects. (4/12/2017 The following was fixed in extension version 0.6.1). Ironically enough, this gets auto-generated wrong. It auto-generates to "${config.python.pythonPath}" which is deprecated in the newer Visual Studio Code versions. It might still work, but you should use "${config:python.pythonPath}" instead for your default first python on your path or Visual Studio Code settings. (4/6/2017 Edit: This should be fixed in the next release. The team committed the fix a few days ago.) program: The initial file that you debugger starts up when you hit run. "${workspaceRoot}" is the root folder you opened up as your workspace (When you go over to the file icon, the base open folder). Another neat trick if you want to get your program running quickly, or you have multiple entry points to your program is to set this to "${file}" which will start debugging at the file you have open and in focus in the moment you hit debug. cwd: The current working directory folder of the project you're running. Usually you'll just want to leave this "${workspaceRoot}". debugOptions: Some debugger flags. The ones in the picture are default flags, you can find more flags in the python debugger pages, I'm sure. args: This isn't actually a default configuration setting, but a useful one nonetheless (and probably what the OP was asking about). These are the command line arguments that you pass in to your program. The debugger passes these in as though they you had typed: python file.py [args] into your terminal; passing each JSON string in the list to the program in order.

有关Visual Studio Code文件变量的更多信息,可用于配置调试器和路径。

您可以到这里查看扩展自己的启动选项文档,包括可选属性和必选属性。

如果文件中没有配置模板,可以单击右下角的Add Configuration按钮。它将为您提供一个列表,以便为大多数常见的调试过程自动生成配置。

现在,根据vlad的回答,您可以按照正常的可视化调试器添加任何您需要的断点,在左上角的下拉菜单中选择您想要的运行配置,您可以点击配置名称左侧的绿色箭头来启动您的程序。

专业提示:团队中不同的人使用不同的ide,他们可能不需要您的配置文件。Visual Studio Code几乎总是把它的IDE文件放在一个地方(设计为这个目的;如果这是你第一次生成Visual Studio Code文件,请确保将.vscode/目录添加到你的.gitignore中(如果你还没有这个文件,这个过程将在你的工作空间中创建文件夹)!

我使用Python 3.7(32位)。要在Visual Studio Code中运行程序,我右键单击程序并选择“在Python交互式窗口中运行当前文件”。如果你没有Jupyter,你可能会被要求安装它。