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

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

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

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


当前回答

在VSCode的新版本(2019年和更新版本)中,我们已经为python运行和调试按钮,

调试:F5

不调试运行:Ctrl + F5

你可以通过文件>首选项>键盘快捷键来改变它 搜索RUN: start Without Debugging并将快捷方式更改为您想要的。 它是如此简单,为我工作(我的VSCode版本是1.51,但新的更新可用)。

其他回答

我使用我现有的anaconda环境来运行python。而不是使用python用户appdata\local\programs\python使用anaconda install python by environment。这将使您能够访问环境中的所有库。

 1. View->Command Palette->Open user settings
 2. search python
 a. Python: default interpreter path = c:\users\yourname\Anaconda3\python.exe
 b. save the file
 3. View->Command Palette->python:select interpreter
 a. arrow down to your workspace name
 b. select your python and environment

 create a python script and run it.

 see https://code.visualstudio.com/docs/python/environments

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

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

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

超级简单:

按下F5键,代码将运行。

如果设置了断点,按F5将在断点处停止,并在调试模式下运行代码。

其他方法-添加快捷方式

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

选择文件→首选项→键盘快捷键(也可以按Ctrl + K + S) 在搜索框中输入python. execterminal 双击该结果(或者,可以单击加号图标) 按Ctrl + Alt + B将其注册为键绑定(或者,您可以输入自己的键绑定)

现在您可以关闭键盘快捷键选项卡 转到您想要运行的Python文件,并按Ctrl + Alt + B(或者,您可以按下您设置的键绑定)来运行它。输出将显示在底部的终端选项卡中。

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

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