在windows下使用vscode开发python
一.配置python环境
方法1.使用cmd安装(部分windows用户可以使用)
打开cmd输入python回车后,自动进入Microsoft Store,点击获取后即可安装python3.7环境。图中的是已经安装好的
方法二:直接到官网下载安装就行,具体方法百度上过多不多赘述,不会的请自行百度
附上下载链接:https://www.python.org/getit/
二.下载,安装Visual Studio Code
1.下载Visual Studio Code
官网地址:https://code.visualstudio.com/
点击Download for Windows即可下载
安装Visual Studio Code
根据自己的习惯选择需要的功能选项,选择添加到path的注意重启电脑(个人习惯全选)
三.安装插件,配置VScode
1.常用插件配置
1.安装中文插件
方法1,点击下图红圈所示的图标
方法2,使用组合键Ctrl+Shift+X
在搜索框中输入Chinese,选择安装第一个插件,点击install下载安装
在右下脚弹出的对话框中点击restart now重启vscode
重启后安装汉化补丁安装完成
2.创建自己的工作区配置vscode
点击打开文件夹选择自己想存储的位置点击选择文件夹,进入到资源管理器中,创建 .py文件
创建方法:点击红圈位置输入名字(注:写上文件扩展.py)
创建成功后,右下脚弹出对话框选择第一个选项,安装python插件,点击安装或者使用组合键Ctr + Shift+X 搜索python选择第一个安装
3.添加配置
如图,选择左边的第三个图标,在点击“添加配置”添加配置文件,并选择Python选项,弹出launch.json文件的编辑框
将下列代码粘贴到launch.json文件中,Ctr + S保存文件,此时配置环境出多出两个环境Python:Debug和Python。使用Python:Debug环境时,可以在Debug窗口显示的从上到下依次是调试所用的模式;python为正常模式
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python:Debug",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
},
{
"name": "Python",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"host": "localhost",
"port": 3000
}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
附上部分快捷键
启动调试F5
不调试启动Ctrl + F5
停止调试Shift + F5
切换断点F9
单步跳过F10
单步执行F11
单步停止Shift + F11
4.配置生成
如图所示,回到资源管理器使用组合键Ctrl + Shift+ B弹出对话框,选择配置生成任务,使用模板创建tasks.json.文件,Others运行任意外部命令的示例
将下列代码粘贴在tasks.json.文件中,保存
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "VScode",
"type": "shell",
// "command"为python.exe的绝对路径
"command": "C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1008.0_x64__qbz5n2kfra8p0\\python.exe",
"args": [
"${file}"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
再次使用组合键盘Ctrl + Shift+ B执行时,如下图所示
5.配置settings.json文件(对于使用vscode开发python的体验至关重要)
点击文件,首选项,设置,扩展,在settings.json编辑
将下列代码粘贴到settings.json中,保存
{
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"python.jediEnabled": true,
"python.autoComplete.addBrackets": true,
"python.linting.enabled": true,
"python.formatting.provider": "yapf",
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 100,
"editor.fontSize": 18,
"workbench.iconTheme": "vscode-icons",
"workbench.colorTheme": "One Dark Pro Bold",
"fileheader.configObj": {
"createFileTime": true,
"timeNoDetail": false,
"language": {
"languagetest": {
"head": "/$$",
"middle": " $ @",
"end": " $/"
}
},
"autoAdd": false,
"autoAlready": true,
"annotationStr": {
"head": "/*",
"middle": " * @",
"end": " */",
"use": true
},
"headInsertLine": {
"php": 2
},
"beforeAnnotation": {
"py": "#!/usr/bin/env python\n# coding=UTF-8"
},
"specialOptions": {}
},
"fileheader.customMade": {
"Author": "user",
"Date": "Do not edit",
"LastEditors": "user",
"LastEditTime": "Do not Edit",
"Descripttion": "",
},
"fileheader.cursorMode": {
"description": "",
"param": "",
"return": ""
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
其中"python.linting.flake8Enabled": 设置为true,使用flake8代码检测
“python.linting.pylintEnabled”: 设置为false,关闭pylint代码检测
"python.jediEnabled"设置为true,函数自动补全
“python.autoComplete.addBrackets”:设置为 true,使用函数自动加括号,方便快捷
“python.linting.enabled”:使用flake8的必须设置为true,否则无法使用flake8
"python.formatting.provider"格式化代码可选 ‘autopep8’, ‘black’, and ‘yapf’.看个人喜欢
“files.autoSave”: “afterDelay"和"files.autoSaveDelay”: 100 保存这个数值,且必须使用,否则代码无及时查错,影响编程手感
"fileheader.customMade"和"fileheader.cursorMode"为头部注释和函数注释,需下载插件koroFileHeader才能使用
使用方法
头部注释:Ctrl/Command + Alt+ I
函数注释:Ctrl/Command + Alt+ T
6.部分插件推荐
1.Anaconda Extension Pack(python的Anaconda扩展)
2.Bracket Pair Colorizer(分清括号的位置)
3.filesize(在底部状态栏显示当前文件大小,点击后还可以看到详细创建、修改时间)
4.Git History(git源代码管理插件)
5.GitLens — Git supercharged(配合Git History使用)
6.indent-rainbow(一个简单的插件可以使得对齐更加具有可读性)
7.Indenticator(缩进深度显示)
8.MagicPython(函数高亮)
9.One Dark Pro(vscode主题,色彩很舒服)
10.Output Colorizer(输出着色)
11.Path Autocomplete(补全文件路径)
12.Python for VSCode(显示函数定义)
13.TODO Highlight
14.Todo Tree(TODO注释标记,配合TODO Highlight才能使用)
15.vscode-icons(文件图标)
</div>
在windows下使用vscode开发python
一.配置python环境
方法1.使用cmd安装(部分windows用户可以使用)
打开cmd输入python回车后,自动进入Microsoft Store,点击获取后即可安装python3.7环境。图中的是已经安装好的
方法二:直接到官网下载安装就行,具体方法百度上过多不多赘述,不会的请自行百度
附上下载链接:https://www.python.org/getit/
二.下载,安装Visual Studio Code
1.下载Visual Studio Code
官网地址:https://code.visualstudio.com/
点击Download for Windows即可下载
安装Visual Studio Code
根据自己的习惯选择需要的功能选项,选择添加到path的注意重启电脑(个人习惯全选)
三.安装插件,配置VScode
1.常用插件配置
1.安装中文插件
方法1,点击下图红圈所示的图标
方法2,使用组合键Ctrl+Shift+X
在搜索框中输入Chinese,选择安装第一个插件,点击install下载安装
在右下脚弹出的对话框中点击restart now重启vscode
重启后安装汉化补丁安装完成
2.创建自己的工作区配置vscode
点击打开文件夹选择自己想存储的位置点击选择文件夹,进入到资源管理器中,创建 .py文件
创建方法:点击红圈位置输入名字(注:写上文件扩展.py)
创建成功后,右下脚弹出对话框选择第一个选项,安装python插件,点击安装或者使用组合键Ctr + Shift+X 搜索python选择第一个安装
3.添加配置
如图,选择左边的第三个图标,在点击“添加配置”添加配置文件,并选择Python选项,弹出launch.json文件的编辑框
将下列代码粘贴到launch.json文件中,Ctr + S保存文件,此时配置环境出多出两个环境Python:Debug和Python。使用Python:Debug环境时,可以在Debug窗口显示的从上到下依次是调试所用的模式;python为正常模式
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python:Debug",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
},
{
"name": "Python",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"host": "localhost",
"port": 3000
}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
附上部分快捷键
启动调试F5
不调试启动Ctrl + F5
停止调试Shift + F5
切换断点F9
单步跳过F10
单步执行F11
单步停止Shift + F11
4.配置生成
如图所示,回到资源管理器使用组合键Ctrl + Shift+ B弹出对话框,选择配置生成任务,使用模板创建tasks.json.文件,Others运行任意外部命令的示例
将下列代码粘贴在tasks.json.文件中,保存
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "VScode",
"type": "shell",
// "command"为python.exe的绝对路径
"command": "C:\\Program Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.7_3.7.1008.0_x64__qbz5n2kfra8p0\\python.exe",
"args": [
"${file}"
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared",
"showReuseMessage": true,
"clear": false
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
再次使用组合键盘Ctrl + Shift+ B执行时,如下图所示
5.配置settings.json文件(对于使用vscode开发python的体验至关重要)
点击文件,首选项,设置,扩展,在settings.json编辑
将下列代码粘贴到settings.json中,保存
{
"python.linting.flake8Enabled": true,
"python.linting.pylintEnabled": false,
"python.jediEnabled": true,
"python.autoComplete.addBrackets": true,
"python.linting.enabled": true,
"python.formatting.provider": "yapf",
"files.autoSave": "afterDelay",
"files.autoSaveDelay": 100,
"editor.fontSize": 18,
"workbench.iconTheme": "vscode-icons",
"workbench.colorTheme": "One Dark Pro Bold",
"fileheader.configObj": {
"createFileTime": true,
"timeNoDetail": false,
"language": {
"languagetest": {
"head": "/$$",
"middle": " $ @",
"end": " $/"
}
},
"autoAdd": false,
"autoAlready": true,
"annotationStr": {
"head": "/*",
"middle": " * @",
"end": " */",
"use": true
},
"headInsertLine": {
"php": 2
},
"beforeAnnotation": {
"py": "#!/usr/bin/env python\n# coding=UTF-8"
},
"specialOptions": {}
},
"fileheader.customMade": {
"Author": "user",
"Date": "Do not edit",
"LastEditors": "user",
"LastEditTime": "Do not Edit",
"Descripttion": "",
},
"fileheader.cursorMode": {
"description": "",
"param": "",
"return": ""
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
其中"python.linting.flake8Enabled": 设置为true,使用flake8代码检测
“python.linting.pylintEnabled”: 设置为false,关闭pylint代码检测
"python.jediEnabled"设置为true,函数自动补全
“python.autoComplete.addBrackets”:设置为 true,使用函数自动加括号,方便快捷
“python.linting.enabled”:使用flake8的必须设置为true,否则无法使用flake8
"python.formatting.provider"格式化代码可选 ‘autopep8’, ‘black’, and ‘yapf’.看个人喜欢
“files.autoSave”: “afterDelay"和"files.autoSaveDelay”: 100 保存这个数值,且必须使用,否则代码无及时查错,影响编程手感
"fileheader.customMade"和"fileheader.cursorMode"为头部注释和函数注释,需下载插件koroFileHeader才能使用
使用方法
头部注释:Ctrl/Command + Alt+ I
函数注释:Ctrl/Command + Alt+ T
6.部分插件推荐
1.Anaconda Extension Pack(python的Anaconda扩展)
2.Bracket Pair Colorizer(分清括号的位置)
3.filesize(在底部状态栏显示当前文件大小,点击后还可以看到详细创建、修改时间)
4.Git History(git源代码管理插件)
5.GitLens — Git supercharged(配合Git History使用)
6.indent-rainbow(一个简单的插件可以使得对齐更加具有可读性)
7.Indenticator(缩进深度显示)
8.MagicPython(函数高亮)
9.One Dark Pro(vscode主题,色彩很舒服)
10.Output Colorizer(输出着色)
11.Path Autocomplete(补全文件路径)
12.Python for VSCode(显示函数定义)
13.TODO Highlight
14.Todo Tree(TODO注释标记,配合TODO Highlight才能使用)
15.vscode-icons(文件图标)
</div>
来源:https://blog.csdn.net/wangh0802/article/details/99547475