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

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

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

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


当前回答

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

其他回答

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

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

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

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

有一个最简单的方法来创建run in terminal命令的快捷方式:

点击左边栏上的设置图标。 然后点击键盘快捷键。 粘贴python。execInTerminal在搜索栏顶部。 现在双击Python: Run Python File in Terminal命令对面的Keybinding列并设置快捷方式。

下面是如何在Visual Studio Code中配置任务运行器以运行.py文件。

在控制台中,按Ctrl + Shift + P (Windows)或Cmd + Shift + P (Apple)。这将出现一个搜索框,您可以在其中搜索“配置任务运行器”

如果这是第一次打开“任务:配置任务运行器”,则需要在下一个选择列表底部选择“其他”。

这将打开属性,然后您可以更改以适应您的偏好。在本例中,您希望更改以下属性;

将Command属性从"tsc" (TypeScript)更改为"Python" 将showOutput从"silent"修改为"Always" 将args (Arguments)从["Helloworld. "Ts "]到["${文件}"](文件名) 删除最后一个属性problemMatcher 保存所做的更改

您现在可以打开您的.py文件,并使用快捷键Ctrl + Shift + B (Windows)或Cmd + Shift + B (Apple)很好地运行它。

注意:你必须在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.