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

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

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

是否有一种方法可以在Visual Studio code中执行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.

其他回答

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


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

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

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

您可以添加一个自定义任务来完成此任务。下面是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中配置任务运行器以运行.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)很好地运行它。

所有这些答案现在都过时了。

目前你必须:

安装Python语言扩展(当然是Python) 打开文件夹(重要!),打开文件夹内的任何Python文件 切换到调试“tab”(?),然后点击变速箱(提示'Configure of Fix 'launch.json ") 保存打开的启动。Json文件(它被放置在步骤#2中打开的文件夹中的.vscode子目录中) 最后,点击绿色三角形或按F5

没有额外的扩展或手动启动。现在需要编辑Json。