vscode 配置 python3開發(fā)環(huán)境的方法
vscode來寫python,配置靈活,界面美觀,是個非常好的選擇。我這里是在ubuntu系統(tǒng)下配置vscode的python3開發(fā)環(huán)境,當(dāng)然也可以參照本文在其它操作系統(tǒng)下配置vscode的python開發(fā)環(huán)境。
1 安裝插件
python

這個是vscode提供的python 官方插件,提供了python代碼的調(diào)試,自動補全,代碼格式化等功能
vscode-icons

這個也是vscode官方提供的插件,作用是給vscode編輯的文件增加圖標(biāo)。這里再推薦一個相同功能的插件**vscode-icons-mac**,文件圖標(biāo)變成Mac風(fēng)格,相當(dāng)美觀。
Path Intellisense

這個插件的作用是當(dāng)代碼中讀入文件名或者文件路徑時,提供文件名或者文件路徑的自動補全
topper

這個插件的作用是在.pyw文件的開頭添加一些說明header
Bracket Pair Colorizer

這個插件的作用是給代碼中的括號增加顏色,同一對括號是相同的顏色,尤其是在括號中還包著括號的時候,看起來更加的清晰。
2 配置
可以在 這里下載我的配置文件,直接放在自己的python工作空間中。windows下的用戶可以這里下載我的配置文件。
2.1 建立Python文件夾
vscode 是基于文件夾的編輯器,我們可以首先建立一個文件夾叫做PYTHON,作為我們的Python編程工作空間,只要一次配置好了這個工作空間,以后這個工作空間的配置就會對它之下的所有的.py 文件都起作用。
打開vscode,點擊左上角文件 —> 打開文件夾,然后打開剛剛建立的PYTHON 文件夾。
然后我們點擊PYTHON 文件夾右邊的添加文件按鈕:

添加一個.py 文件,名字叫做hellovscode.py .

2.2 配置launch.json 文件
點擊菜單欄調(diào)試 —> 打開配置,就會彈出一個選擇框,我們在這里要選擇Python,然后就打開了launch.json 文件:

我們看到的launch.json 文件中的內(nèi)容如上圖所示。同時我們還發(fā)現(xiàn),在python工作區(qū)PYTHON下面還多了一個文件夾.vscode, 而且launch.json 就在這個文件夾中。
對launch.json 文件的配置如下:
在"configurations": [] z中,對于第一個{ }內(nèi)的內(nèi)容修改如下:
{
"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"
]
}
]
}
后面幾個{ }中的內(nèi)容修改如下:
{
"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 文件
點擊菜單欄任務(wù) —> 配置任務(wù),就會彈出一個選擇框,我們在這里要選擇使用模板創(chuàng)建tasks.json文件,然后又彈出一個選擇框,這里選擇Others,就打開了tasks.json 文件:

對tasks.json 文件的配置如下:
{
"version": "2.0.0",
"tasks": [
{
"label": "python3",
"type": "shell",
"command": "/usr/bin/python3",
"args": ["${file}"]
}
]
}
2.4 用戶設(shè)置
點擊菜單欄文件 —> 首選項—> 設(shè)置,然后打開用戶設(shè)置:

用戶設(shè)置如下:
{
"git.ignoreLegacyWarning": true,
"workbench.iconTheme": "vscode-icons", //啟用vscode圖標(biāo)
"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",
}
接下來為topper配置一個快捷鍵以便于在python文件中快速插入文件header。
打開文件->首選項->鍵盤快捷方式:

在搜索框輸入topper

點擊要配置的命令,然后輸入想要設(shè)定的快捷鍵,例如我對topper.addTopHeader.persionalProfile設(shè)置的快捷鍵為Crtl+T T。
那么當(dāng)在一個python文件中按下Crtl+T T時,就會插入header:

配置完畢,可以在vscode中愉快的寫python了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python Django連接MySQL數(shù)據(jù)庫做增刪改查
本文寫的是python Django連接MySQL數(shù)據(jù)庫的步驟,提供增刪改查的代碼2013-11-11
python的scipy.stats模塊中正態(tài)分布常用函數(shù)總結(jié)
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python的scipy.stats模塊中正態(tài)分布常用函數(shù)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02
Python利用memory_profiler查看內(nèi)存占用情況
memory_profiler是第三方模塊,用于監(jiān)視進程的內(nèi)存消耗以及python程序內(nèi)存消耗的逐行分析。本文將利用memory_profiler查看代碼運行占用內(nèi)存情況,感興趣的可以了解一下2022-06-06

