Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
当前回答
在windows vs code上运行python3:
从官方网站下载python解释器 安装vs code的python包。这可以直接从vscode的扩展管理器中安装 通过运行以下命令,验证您的python3已经安装: Py -3—版本 在vscode的终端上使用以下命令运行脚本: Py -3 main.py
要了解更多信息,请前往这里了解详细的安装过程。
其他回答
简单直接的Python扩展可以节省时间和精力。 检测、调试、代码完成是安装完成后可用的特性。在此之后,要运行代码,需要配置正确的Python安装路径以运行代码。一般设置可在“用户作用域”和“工作空间”中为Python语言-“Python”配置。pythonPath环境”:“c: / python27 / python.exe” 通过以上步骤,至少可以执行基本的Python程序。
我已经通过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中安装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.
在Visual Studio Code (Python)的最新版本(1.36)中:
按F5,然后按Enter,在集成终端中运行代码。
按Ctrl + A,然后按Shift + Enter,在交互式IPython shell中运行代码。
如果你为VSCode安装了Python语言扩展,它默认也会安装Jupyter和Pylance,这让你可以以交互的方式运行Python代码。
您所要做的就是在希望以交互方式执行的代码之前使用# %%。
只要你插入# %%,你就可以看到VSCode为你创建了一个新的Jupyter Cell。
在那里你可以点击运行单元格菜单,你可以看到结果。
所以,你所要做的就是在你的VSCode中输入以下代码,
# %%
text = 'Hello World from inline interactive Python'
print(text)