vscode來寫python,配置靈活,界面美觀,是個很是好的選擇。我這裏是在ubuntu系統下配置vscode的python3開發環境,固然也能夠參照本文在其它操做系統下配置vscode的python開發環境。python
1 安裝插件
2 配置
2.1 創建Python文件夾
2.2 配置launch.json 文件
2.3 配置tasks.json 文件
2.4 用戶設置
1 安裝插件
python
這個是vscode提供的python 官方插件,提供了python代碼的調試,自動補全,代碼格式化等功能
vscode-icons
這個也是vscode官方提供的插件,做用是給vscode編輯的文件增長圖標。
Path Intellisense
這個插件的做用是當代碼中讀入文件名或者文件路徑時,提供文件名或者文件路徑的自動補全
topper
這個插件的做用是在.pyw文件的開頭添加一些說明header
Guides
這個插件的做用是增長.py 中的指示線,用這個插件能讓代碼的層次結構更加清晰。
Bracket Pair Colorizer
這個插件的做用是給代碼中的括號增長顏色,同一對括號是相同的顏色,尤爲是在括號中還包着括號的時候,看起來更加的清晰。
2 配置
能夠在 這裏下載個人配置文件,直接放在本身的python工做空間中。git
2.1 創建Python文件夾
vscode 是基於文件夾的編輯器,咱們能夠首先創建一個文件夾叫作PYTHON,做爲咱們的Python編程工做空間,只要一次配置好了這個工做空間,之後這個工做空間的配置就會對它之下的全部的.py 文件都起做用。github
打開vscode,點擊左上角文件 —> 打開文件夾,而後打開剛剛創建的PYTHON 文件夾。
而後咱們點擊PYTHON 文件夾右邊的添加文件按鈕:
web
添加一個.py 文件,名字叫作hellovscode.py . shell
2.2 配置launch.json 文件
點擊菜單欄調試 —> 打開配置,就會彈出一個選擇框,咱們在這裏要選擇Python,而後就打開了launch.json 文件: django
咱們看到的launch.json 文件中的內容如上圖所示。同時咱們還發現,在python工做區PYTHON下面還多了一個文件夾.vscode, 並且launch.json 就在這個文件夾中。
對launch.json 文件的配置以下:編程
在"configurations": [] z中,對於第一個{ }內的內容修改以下:json
{
"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"
]
}
]
}
後面幾個{ }中的內容修改以下:ubuntu
{
"name": "Python: Terminal (integrated)",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "/usr/bin/python3",
"program": "${file}",
"cwd": "",
"console": "integratedTerminal",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": []
},
{
"name": "Python: Terminal (external)",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "/usr/bin/python3",
"program": "${file}",
"cwd": "",
"console": "externalTerminal",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions": []
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "/usr/bin/python3",
"program": "${workspaceFolder}/manage.py",
"cwd": "${workspaceFolder}",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"env": {},
"envFile": "${workspaceFolder}/.env",
"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}"]
}
]
}
{ "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": "john.doe@doejohn.com"
}
}
],
"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": "#",
"template": [
"${headerBegin}",
"${headerPrefix} ${fileName}",
"${headerPrefix} @author ${author}",
"${headerPrefix} @description ${description}",
"${headerPrefix} @created ${createdDate}",
"${headerPrefix} @last-modified ${lastModifiedDate}",
"${headerEnd}"
]
}
}
],
"editor.fontFamily": "monospace",
"terminal.integrated.fontFamily": "monospace",
"editor.fontWeight": "500",
{ "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": "john.doe@doejohn.com" } } ], "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": "#", "template": [ "${headerBegin}", "${headerPrefix} ${fileName}", "${headerPrefix} @author ${author}", "${headerPrefix} @description ${description}", "${headerPrefix} @created ${createdDate}", "${headerPrefix} @last-modified ${lastModifiedDate}", "${headerEnd}" ] } } ], "editor.fontFamily": "monospace", "terminal.integrated.fontFamily": "monospace", "editor.fontWeight": "500", }
配置完畢,能夠在vscode中愉快的寫python了。