vscode 配置 python3開發環境的方法
vscode來寫python,配置靈活,介面美觀,是個非常好的選擇。我這裡是在ubuntu系統下配置vscode的python3開發環境,當然也可以參照本文在其它作業系統下配置vscode的python開發環境。
1 安裝外掛
python
這個是vscode提供的python 官方外掛,提供了python程式碼的除錯,自動補全,程式碼格式化等功能
vscode-icons
這個也是vscode官方提供的外掛,作用是給vscode編輯的檔案增加圖示。這裡再推薦一個相同功能的外掛**vscode-icons-mac
**,檔案圖示變成Mac風格,相當美觀。
Path Intellisense
這個外掛的作用是當代碼中讀入檔名或者檔案路徑時,提供檔名或者檔案路徑的自動補全
topper
這個外掛的作用是在.py
w檔案的開頭新增一些說明header
Bracket Pair Colorizer
這個外掛的作用是給程式碼中的括號增加顏色,同一對括號是相同的顏色,尤其是在括號中還包著括號的時候,看起來更加的清晰。
2 配置
可以在 這裡下載我的配置檔案,直接放在自己的python工作空間中。windows下的使用者可以這裡下載我的配置檔案。
2.1 建立Python資料夾
vscode 是基於資料夾的編輯器,我們可以首先建立一個資料夾叫做PYTHON
,作為我們的Python程式設計工作空間,只要一次配置好了這個工作空間,以後這個工作空間的配置就會對它之下的所有的.py
開啟vscode,點選左上角檔案
—> 開啟資料夾
,然後開啟剛剛建立的PYTHON
資料夾。
然後我們點選PYTHON
資料夾右邊的新增檔案
按鈕:
新增一個.py
檔案,名字叫做hellovscode.py
.
2.2 配置launch.json 檔案
點選選單欄除錯
—> 開啟配置
,就會彈出一個選擇框,我們在這裡要選擇Python
,然後就打開了launch.json
檔案:
我們看到的launch.json
檔案中的內容如上圖所示。同時我們還發現,在python工作區PYTHON
下面還多了一個資料夾.vscode
,而且launch.json
就在這個資料夾中。
對launch.json
檔案的配置如下:
在"configurations": []
z中,對於第一個{ }內的內容修改如下:
{ "version": "0.2.0","configurations": [ { "name": "Python3","type": "python","request": "launch","stopOnEntry": false,"pythonPath": "/usr/bin/python3",//python3的安裝路徑 "program": "${file}","cwd": "${workspaceFolder}","env": {},"envFile": "${workspaceFolder}/.env","debugOptions": [ "RedirectOutput" ] } ] }
後面幾個{ }中的內容修改如下:
{ "name": "Python: Terminal (integrated)","program": "${file}","cwd": "","console": "integratedTerminal","debugOptions": [] },{ "name": "Python: Terminal (external)","console": "externalTerminal",{ "name": "Python: Django","stopOnEntry": true,"program": "${workspaceFolder}/manage.py","args": [ "runserver","--noreload","--nothreading" ],"debugOptions": [ "RedirectOutput","Django" ] },
其它地方都不用修改。
2.3 配置tasks.json 檔案
點選選單欄任務
—> 配置任務
,就會彈出一個選擇框,我們在這裡要選擇使用模板建立tasks.json檔案
,然後又彈出一個選擇框,這裡選擇Others
,就打開了tasks.json
檔案:
對tasks.json
檔案的配置如下:
{ "version": "2.0.0","tasks": [ { "label": "python3","type": "shell","command": "/usr/bin/python3","args": ["${file}"] } ] }
2.4 使用者設定
點選選單欄檔案
—> 首選項
—> 設定
,然後開啟使用者設定
:
使用者設定如下:
{ "git.ignoreLegacyWarning": true,"workbench.iconTheme": "vscode-icons",//啟用vscode圖示 "python.pythonPath": "/usr/bin/python3",// python3路徑 "editor.lineHeight": 26,// 編輯器中的行高 "editor.fontSize": 18,// 編輯器中的字型 "editor.wordWrap": "on","editor.formatOnSave": true,//編輯器自動儲存 "python.linting.flake8Enabled": true,//啟用flake8,首先需要pip3 install falke8 "python.formatting.provider": "yapf",///啟用yapf,首先需要pip3 install yapf "editor.renderIndentGuides": false,"path-intellisense.autoSlashAfterDirectory": true,"path-intellisense.extensionOnImport": true,"workbench.colorTheme": "Monokai",// 配色方案 "python.linting.pylintArgs": [ "--load-plugins","pylint_django","--disable-msg=C0111" ],// 忽略的警告資訊 // 下面是topper的插入header配置 "topper.customTemplateParameters": [ { "personalProfile": { "author": "你的名字","website": "bulbasaur.github.bitbucket.yababbdadado.com","copyright": "None \n None","license": "None","email": "你的郵箱" } },{ "officeProfile": { "author": "John Doe","department": "Product Development","email": "[email protected]" } } ],"topper.headerTemplates": [ { "defaultCStyled": { "headerBegin": "/**","headerPrefix": "*","headerEnd": "*/","template": [ "${headerBegin}","${headerPrefix} ${fileName}","${headerPrefix} @author ${author}","${headerPrefix} @description ${description}","${headerPrefix} @created ${createdDate}","${headerPrefix} @copyright ${copyright}","${headerPrefix} @last-modified ${lastModifiedDate}","${headerEnd}" ] } },{ "python": { "headerBegin": "# -*- coding: utf-8 -*-","headerPrefix": "#","headerEnd": "#","${headerEnd}" ] } } ],"editor.fontFamily": "monospace","terminal.integrated.fontFamily": "monospace","editor.fontWeight": "500",}
接下來為topper配置一個快捷鍵以便於在python檔案中快速插入檔案header。
開啟檔案
->首選項
->鍵盤快捷方式
:
在搜尋框輸入topper
點選要配置的命令,然後輸入想要設定的快捷鍵,例如我對topper.addTopHeader.persionalProfile
設定的快捷鍵為Crtl+T T
。
那麼當在一個python檔案中按下Crtl+T T
時,就會插入header:
配置完畢,可以在vscode中愉快的寫python了。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。