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

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

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

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


当前回答

如果你有一个由多个Python文件组成的项目,并且你想用主程序开始运行/调试,而不依赖于当前的哪个文件,你可以创建以下启动配置(将MyMain.py更改为你的主文件)。

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: Main File",
      "type": "python",
      "request": "launch",
      "program": "${workspaceFolder}/MyMain.py",
      "console": "integratedTerminal",
      "cwd": "${workspaceFolder}"
    }
  ]
}

其他回答

安装Python扩展(Python应该安装在您的系统中)。要安装Python扩展,请按Ctrl + Shift + X,然后输入' Python '并输入。安装扩展。 打开包含Python代码的文件。是的!一个.py文件。 现在要运行.py代码,只需在编辑器屏幕上右键单击并点击“在终端中运行Python文件”。就是这样!

这是额外的一步。事实上,一次又一次的点击让我很恼火,所以我设置了快捷键。

点击左下角的“设置类型”图标→键盘快捷键→键入“在终端中运行Python文件”。现在你会看到+号,然后选择快捷方式。你已经完成了!

注意:你必须在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的一个基本自定义任务。

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

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

到目前为止,在Visual Studio Code中运行Python有四种方式:

通过集成终端(拜托,它是集成的!所以从技术上讲,你可以在Visual Studio Code中运行它;)

不需要安装任何扩展。 不需要创建和配置任何东西(假设您的$PATH中已经有python)。 ⌃空格(打开终端)和python my_file.py(运行文件)。

通过自定义任务(接受芬顿的回答):

不需要安装任何扩展。 默认的Visual Studio Code做事的方式。 注意不要复制粘贴答案,因为它的problemMatcher.pattern.regexp已经损坏,并且挂起了编辑器。最好要么删除problemMatcher,要么将regexp至少更改为^\\s+(.*)$。

通过代码运行扩展(@JanHan的答案):

需要配置代码运行器。在用户设置中的executorMap(添加路径到您的python)。 非常有用的扩展,特别是如果你在Visual Studio代码中不仅运行Python。

通过微软的官方Python扩展(vlad2135的答案):

需要创建launch.js(在Visual Studio Code的Debug选项卡中单击几下)。 对于那些想要使用Visual Studio Code作为Python的主要IDE的人来说,这个扩展是必不可少的。

我已经通过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文件将自动运行并显示输出:)