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」,輸入如下內容並保存:python

      { // 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
}
  • 完整工做區配置文件
  1. //將設置放入此文件中以覆蓋默認值和用戶設置。
  2. {
  3. "python.pythonPath":"${workspaceRoot}/venv/bin/python",
  4. "editor.renderWhitespace":"all",
  5. "python.linting.pylintEnabled": false,
  6. "python.linting.flake8Enabled": true,
  7. "python.formatting.provider":"yapf",
  8. //配置 glob 模式以排除文件和文件夾。
  9. "files.exclude":{
  10. "**/.git": true,
  11. "**/.svn": true,
  12. "**/.hg": true,
  13. "**/.DS_Store": true,
  14. "**/*.pyc":true
  15. }
  16. }
  • 配置代碼片斷
    • Code—首選項—用戶代碼片斷,選擇python
    • 在配置文件中,輸入想要定義的內容,字段含義以下:
prefix :這個參數是使用代碼段的快捷入口,好比這裏的log在使用時輸入log會有智能感知. body :這個是代碼段的主體.須要設置的代碼放在這裏,字符串間換行的話使用\r\n換行符隔開.注意若是值裏包含特殊字符須要進行轉義. $1 :這個爲光標的所在位置. $2 :使用這個參數後會光標的下一位置將會另起一行,按tab鍵可進行快速切換 description :代碼段描述,在使用智能感知時的描述
    • 完整配置文件實例以下:
  1. {
  2. /*
  3. //Place your snippets forPython here.Each snippet is defined under a snippet name and has a prefix, body and
  4. // description.The prefix is what is used to trigger the snippet and the body will be expanded and inserted.Possible variables are:
  5. // $1, $2 for tab stops, $0 for the final cursor position,and ${1:label}, ${2:another}for placeholders.Placeholderswith the
  6. // same ids are connected.
  7. //Example:
  8. "Print to console":{
  9. "prefix":"log",
  10. "body":[
  11. "console.log('$1');",
  12. "$2"
  13. ],
  14. "description":"Log output to console"
  15. }
  16. */
  17. "Input Note":{
  18. "prefix":"itne",
  19. "body":[
  20. "'''",
  21. "Function Name : diff_Json_Same",
  22. "Function : 通用比較xx方法",
  23. "Input Parameters: jsonastr,jsonbstr",
  24. "Return Value : None",
  25. "'''"
  26. ],
  27. "description":"Input the class or function notes!"
  28. }
  29. }
    • 調用方式:在python文件中輸入itne回車,則輸入定義代碼片斷
  • 配置快捷鍵
    • Code—首選項—鍵盤映射拓展
  • 配置主題
    • Code—首選項—顏色主題
    • Code—首選項—文件圖標主題
 
 



相關文章
相關標籤/搜索