作者:shark

说明:本文中的图片是 Mac 版本的 VSCode 。菜单布局上会和 windows 版本少有差别。

一、 安装插件 python

image.png

点击 小方块

在搜索框中搜索 python 关键字

点击第一个 左上角出现 ⭐标识的 安装

二、设置 Python 环境

image.png

Python是一种解释型语言,为了运行Python代码并获得Python IntelliSense,您必须告诉VS Code使用哪种解释器。

image.png

此时会列出目前系统下所有的 python解释器,包括虚拟环境的解释器。

image.png

选择相应的解释器后,会在左下角看到当前已经生效的解释器

image.png

三、新建或打开文件

第一次打开,可以在下图的位置 进行 新建文件,打开文件夹等操作

添加工作区域 的意思是,可以把某个文件夹作为一个单独的工作区域,

可以在这个工作区域内写自己的项目代码,可以针对这个工作区域进行一些专门的设置。

image.png

新建

点击 新建文件 会建立一个临时文件,最后可以 control + s 进行保存

image.png

打开文件夹

这样会让你选择系统中已经存在的文件夹,你可以选择任意一个文件夹作为项目都根目录,或者选择之前的项目根目录。

image.png

已经打开的文件夹会出现在左侧栏中

image.png

假如文件夹中非空,可以点击其左侧开头处的小三角符号,就可以展开文件夹的目录结构了。

image.png

可以选择相应的图标,在此文件夹下创建新文件或创建新文件夹

image.png

输入文件名,敲回车后会自动打开这个文件

image.png

插播一条广告,免费课程,扫码即可免费参加

image.png

image.png

给有缘人推荐一个学习技术的好地方 B 站,相比大家都知道。

但是我发现B 站的大部分视频都是没有成套的,我发现有个地方,有几个成套的,我觉得还可以,我经常会逛逛,我就是从这学的 _

竟然有最新的 Apollo 和 Kong, 讲的还可以,关键是成套的,是企业中的在职人讲的

手机扫描或者点我直达

image.png

四、编写代码

右侧是当前已经打开文件的编辑区域

image.png

image.png

五、 运行代码

1. 右键选择 在终端中运行 python 文件

image.png

image.png

2. 使用一键运行代码的插件运行代码更方便

1. 在插件商店搜索 code runner, 之后安装

image.png

2. 配置 Code Runner

image.png

image.png

image.png

配置默认语言

必须为 Code Runner 配置一个默认语言,可以写 python。 不识别 python3

用户的 settings.json 文件中设置项为:

"code-runner.defaultLanguage": "python",

对应的图:

image.png

配置全局的针对不同语言代码的解释器

这个图形的没有,必须在用户的 settings.json 文件中配置。

通过识别不同点文件扩展名,使用配置好的解释器运行此文件。

"code-runner.executorMapByGlob": {
"*.js": "node"
},

配置工作区的解释器

工作区的配置文件为每个工作区中的 settings.json。这个文件在工作区根目录下的 .vscode 隐藏文件夹下。

image.png
"code-runner.executorMap": {
"python": "/Users/yanshunjun/.virtualenvs/QF-Online2/bin/python3"
},

六、调试代码

VSCode 商店的 python 插件,就是刚才我们安装的插件,本身就具备调试功能。

1. 打断点

断点就是,当程序运行到此处时,程序会暂停到此处。

打断点的方法:

鼠标点击行号前面,此时会出现一个小红点

image.png

2. 打开调试器

鼠标点击左侧的 "杀虫剂图标" 就可以打开调试模式了

image.png

3. 配置调试器

image.png

image.png

image.png

4. 运行调试器

按下 F5 或者点击小绿箭头

image.png

黄色区域的行,表示程序已经运行到需要调试的断点处了

image.png

变量的值会在左侧栏显示

image.png

控制调试的按钮

image.png

一般我会选择 单步调试(F11), 这样代码会从我们的断点处继续一步步的执行

image.png

假设你选择了 继续(F5) ,代码会继续到下一个断点处

image.png
{
"editor.fontSize": 24,
"files.autoSave": "onFocusChange",
"editor.accessibilitySupport": "off",
"terminal.explorerKind": "external",
"terminal.integrated.fontSize": 24,
"debug.console.fontSize": 24,
"workbench.colorTheme": "Atom One Dark",
// "liveServer.settings.donotShowInfoMsg": true,
"workbench.iconTheme": "vscode-icons",
"window.zoomLevel": 1,
"fileheader.Author": "yan.shunjun",
"fileheader.LastModifiedBy": "yan.shunjun",
"[jsonc]": {
"editor.defaultFormatter": "vscode.json-language-features"
},
"diffEditor.ignoreTrimWhitespace": false,
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.executorMap": {
"python": "/Users/yanshunjun/.pyenv/versions/virtual-SharkAPAMP/bin/python"
},
"code-runner.executorMapByGlob": {
"*.js": "node"
},
"python.pythonPath": "/Users/yanshunjun/.pyenv/versions/virtual-SharkAPAMP/bin/python",
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"markdown.preview.fontSize": 24,
"code-runner.clearPreviousOutput": true,
"html.format.endWithNewline": true,
"vetur.format.options.useTabs": true,
"vetur.format.scriptInitialIndent": true,
"[html]": {
"editor.defaultFormatter": "HookyQR.beautify"
},
"explorer.confirmDragAndDrop": false,
"editor.detectIndentation": false,
"editor.tabSize": 2,
"vetur.format.defaultFormatter.js": "none",
"terminal.integrated.copyOnSelection": true,
"terminal.integrated.cursorBlinking": true,
"code-runner.defaultLanguage": "python",
"git.enableSmartCommit": true,
"python.envFile": "$HOME/.virtualenv",
"files.associations": {
"*.html": "html"
},
"terminal.integrated.rendererType": "dom",
"vscode_custom_css.imports": [
"file:///Users/yanshunjun/vscode-css-js/custom.css",
"file:///Users/yanshunjun/vscode-css-js/custom.js"
],
"vscode_custom_css.policy": true,
"code-runner.runInTerminal": true,
"code-runner.saveFileBeforeRun": true,
"vsicons.dontShowNewVersionMessage": true,
"python.insidersChannel": "off"
}

工作区配置文件

{
"code-runner.saveAllFilesBeforeRun": true,
"code-runner.executorMap": {
"python": "/Users/yanshunjun/.pyenv/versions/virtual-SharkAPAMP/bin/python"
},
"code-runner.executorMapByGlob": {
"*.js": "node"
},
"python.pythonPath": "/root/.virtualenv/django2.1/bin/python3"
}

调试配置文件

.vscode/launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: 当前文件",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload"
],
"django": true
}
]
}