Visual Studio Code搭建python開發環境
- python安裝(Mac下自帶)
- Visual Studio Code 安裝
- Visual Studio Code 安裝python插件
- command + P 打開命令輸入界面
- 輸入ext install python 安裝python插件
- 安裝配置flake8(自動錯誤檢查工具)
- python環境中安裝flake8 pip install flake8
- 用戶-首選項-工作區設置中修改配置(用戶設置也可以) "python.linting.flake8Enabled": true
- 安裝配置yapf(自動格式化代碼工具)
- python環境安裝yapf pip install yapf
- 用戶-首選項-工作區設置中修改配置(用戶設置也可以) "python.formatting.provider": "yapf"
- Command + shift + F 格式化代碼
- 配置Command + Shift + B 運行代碼
-
打開或新建一個python源文件,按下快捷鍵
Ctrl+Shift+B
運行,VSC會提示No task runner configured.
,點擊“Configure Task Runner”,選擇“Others”,輸入以下內容並保存:{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "${workspaceRoot}/venv/bin/python", "isShellCommand": true, "args": ["${file}"], "showOutput": "always" }
- 配置vitualenv運行環境
- 用戶-首選項-工作區設置中修改配置(用戶設置也可以)"python.pythonPath": "${workspaceRoot}/venv/bin/python"
- 安裝Linting(代碼格式檢查工具)
- python環境中安裝linting pip install pylint
- 安裝完python插件后pylint是默認開啟的
- 配置顯示空格字符
- 用戶-首選項-工作區設置中修改配置(用戶設置也可以)"editor.renderWhitespace": "all"
- 配置忽略非代碼文件顯示
- 用戶-首選項-工作區設置中修改配置(用戶設置也可以)
"files.exclude":{"**/.git": true,"**/.svn": true,"**/.hg": true,"**/.DS_Store": true,"**/*.pyc":true}
- 完整工作區配置文件
//將設置放入此文件中以覆蓋默認值和用戶設置。
{
"python.pythonPath":"${workspaceRoot}/venv/bin/python",
"editor.renderWhitespace":"all",
"python.linting.pylintEnabled": false,
"python.linting.flake8Enabled": true,
"python.formatting.provider":"yapf",
//配置 glob 模式以排除文件和文件夾。
"files.exclude":{
"**/.git": true,
"**/.svn": true,
"**/.hg": true,
"**/.DS_Store": true,
"**/*.pyc":true
}
}
- 配置代碼片段
- Code—首選項—用戶代碼片段,選擇python
- 在配置文件中,輸入想要定義的內容,字段含義如下:
prefix :這個參數是使用代碼段的快捷入口,比如這里的log在使用時輸入log會有智能感知. body :這個是代碼段的主體.需要設置的代碼放在這里,字符串間換行的話使用\r\n換行符隔開.注意如果值里包含特殊字符需要進行轉義. $1 :這個為光標的所在位置. $2 :使用這個參數后會光標的下一位置將會另起一行,按tab鍵可進行快速切換 description :代碼段描述,在使用智能感知時的描述
- 完整配置文件實例如下:
{
/*
//Place your snippets forPython here.Each snippet is defined under a snippet name and has a prefix, body and
// description.The prefix is what is used to trigger the snippet and the body will be expanded and inserted.Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position,and ${1:label}, ${2:another}for placeholders.Placeholderswith the
// same ids are connected.
//Example:
"Print to console":{
"prefix":"log",
"body":[
"console.log('$1');",
"$2"
],
"description":"Log output to console"
}
*/
"Input Note":{
"prefix":"itne",
"body":[
"'''",
"Function Name : diff_Json_Same",
"Function : 通用比較xx方法",
"Input Parameters: jsonastr,jsonbstr",
"Return Value : None",
"'''"
],
"description":"Input the class or function notes!"
}
}
- 調用方式:在python文件中輸入itne回車,則輸入定義代碼片段
- 配置快捷鍵
- Code—首選項—鍵盤映射拓展
- 配置主題
- Code—首選項—顏色主題
- Code—首選項—文件圖標主題