Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
当前回答
正如Visual Studio Code文档中所述,只需右键单击编辑器中的任何位置,并选择在终端中运行Python文件。
其他回答
如果我只是想在终端中运行Python文件,我会为该命令创建一个键盘快捷方式,因为默认情况下没有快捷方式(你需要在你的路径中有Python解释器可执行文件):
进入首选项(左下角的齿轮图标)→键盘快捷键 输入“在终端中运行Python文件” 点击该命令的“+”,然后输入键盘快捷键
我用Ctrl + Alt + N。
为了使用相应的venv启动当前文件,我在launch.json文件中添加了以下内容:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"pythonPath": "${workspaceFolder}/FOO/DIR/venv/bin/python3"
},
在bin文件夹中存在源代码…/venv/bin/activate脚本,当从常规终端运行时,该脚本是常规来源。
我已经通过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文件将自动运行并显示输出:)
所有这些答案现在都过时了。
目前你必须:
安装Python语言扩展(当然是Python) 打开文件夹(重要!),打开文件夹内的任何Python文件 切换到调试“tab”(?),然后点击变速箱(提示'Configure of Fix 'launch.json ") 保存打开的启动。Json文件(它被放置在步骤#2中打开的文件夹中的.vscode子目录中) 最后,点击绿色三角形或按F5
没有额外的扩展或手动启动。现在需要编辑Json。
我使用我现有的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