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

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

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

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


当前回答

一个解决办法是:

选择所有的行:按Ctrl + A 运行选定的行:Shift + Enter

其他回答

您可以添加一个自定义任务来完成此任务。下面是Python的一个基本自定义任务。

{
    "version": "0.1.0",
    "command": "c:\\Python34\\python",
    "args": ["app.py"],
    "problemMatcher": {
        "fileLocation": ["relative", "${workspaceRoot}"],
        "pattern": {
            "regexp": "^(.*)+s$",
            "message": 1
        }
    }
}

将其添加到文件任务中。并按Ctrl + Shift + B运行它。

我已经通过Anaconda安装了Python。

通过Anaconda启动Visual Studio Code,我能够运行Python程序。

然而,我找不到任何快捷方式(热键)直接运行.py文件。

(使用截至2019年2月21日的最新版本,带有Visual Studio Code附带的Python扩展。 链接:Visual Studio Code的Python扩展)

以下方法起了作用:

右键单击并选择“在终端中运行Python文件”对我来说很有用。 按Ctrl + A然后Shift + Enter(在Windows上)

下面的内容与@jdhao的内容类似。

这是我所做的获得热键:

Ctrl + Shift + B //运行构建任务 它提供了一个配置选项 我点击它以获得更多选项。我点击了其他配置 “任务。Json文件被打开

我让代码看起来像这样:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Run Python File", //this is the label I gave
                "type": "shell",
                "command": "python",
                "args": ["${file}"]

保存后,文件变成这样:

    {
        // See https://go.microsoft.com/fwlink/?LinkId=733558
        // for the documentation about the tasks.json format
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Run Python File",
                "type": "shell",
                "command": "python",
                "args": [
                    "${file}"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }

保存文件` tasks。json',转到你的Python代码并按下 Ctrl + Shift + B。 然后点击运行任务→运行Python文件//这是标签 你给。

现在,每当你按Ctrl + Shift + B, Python文件将自动运行并显示输出:)

关于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了。幸运的是,我在这篇文章中提到的文档非常容易理解。请随意阅读、思考和享受。

注意:你必须在Visual Studio Code中安装Python Extension By Microsoft,并在左下角选择Python解释器。

Go to File → Preferences → Keyboard Shortcuts (alternatively, you can press Ctrl + K + S) In the search box, enter python.execInTerminal Double click that result (alternatively, you can click the plus icon) Press Ctrl + Alt + B to register this as the keybinding (alternatively, you can enter your own keybinding) Now you can close the Keyboard Shortcuts tab Go to the Python file you want to run and press Ctrl + Alt + B (alternatively, you can press the keybinding you set) to run it. The output will be shown in the bottom terminal tab.

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