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

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

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

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


当前回答

为了使用相应的venv启动当前文件,我在launch.json文件中添加了以下内容:

 {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "pythonPath": "${workspaceFolder}/FOO/DIR/venv/bin/python3"
    },

在bin文件夹中存在源代码…/venv/bin/activate脚本,当从常规终端运行时,该脚本是常规来源。

其他回答

为了扩展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中(如果你还没有这个文件,这个过程将在你的工作空间中创建文件夹)!

关于Visual Studio Code任务和调试器有很多困惑。让我们首先讨论它,以便了解何时使用任务,何时使用调试器。

任务

官方文件说-

有很多工具可以自动完成任务,比如检测、构建、 打包、测试或部署软件系统。例子包括 TypeScript编译器,linters,如ESLint和TSLint以及 构建像Make、Ant、Gulp、Jake、Rake和MSBuild这样的系统。 …VS Code中的任务可以配置为运行脚本并启动 过程,以便可以从内部使用许多现有工具 VS Code,而无需输入命令行或编写新代码。

因此,任务不是用来调试、编译或执行程序的。

调试器

如果我们检查调试器文档,就会发现有一种叫做运行模式的东西。上面写着

除了调试程序,VS Code还支持运行 程序。“调试:不启动调试”动作由 Ctrl+F5并使用当前选择的启动配置。许多 在“Run”模式下支持启动配置属性。VS 代码在程序运行时维护一个调试会话,并且 按下停止按钮终止程序。

所以,按F5和Visual Studio Code将尝试调试当前活动的文件。

按Ctrl + F5, Visual Studio Code将忽略断点并运行代码。

配置调试器

要配置调试器,请查阅文档。总之,它说,你应该修改启动。json文件。对于初学者来说,要在集成终端中运行代码(在Visual Studio code中),使用-

{
    "name": "Python: Current File (Integrated Terminal)",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "integratedTerminal"
}

要在外部终端(在Visual Studio code之外)中运行代码,请使用-

{
    "name": "Python: Current File (External Terminal)",
    "type": "python",
    "request": "launch",
    "program": "${file}",
    "console": "externalTerminal"
}

注意:如果所有的文档都很容易搜索和理解,那么我们可能就不需要Stack Overflow了。幸运的是,我在这篇文章中提到的文档非常容易理解。请随意阅读、思考和享受。

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


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

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

在Mac上使用control+F5(fn+F5)

以下是当前(2018年9月)用于运行Python代码的扩展:

官方Python扩展:这是必须安装的。

代码运行器:对各种语言都非常有用,不仅仅是Python。我强烈推荐安装。

实时Python便签,在侧窗口中显示你的变量。我是这个的创建者,所以显然我认为它很棒,但我不能给出一个公正的意见¯\()/¯

Wolf:实时Python便签,内联显示结果

当然,如果你使用集成的终端,你可以在那里运行Python代码,而不必安装任何扩展。