本文以python3.7 為例
一 下載python3
url : https://www.python.org/downloads/windows/
提示: 安裝過程中。記得勾選 添加環境變量
二 、 vscode 安裝所需插件
1 插件名稱: python , 插件功能: 這個是vscode提供的python 官方插件,提供了python代碼的調試,自動補全,代碼格式化等功能
2 插件名稱: vscode-icons , 插件功能: 這個也是vscode官方提供的插件,作用是給vscode編輯的文件增加圖標。
3 插件名稱:Path Intellisense , 插件功能:這個插件的作用是當代碼中讀入文件名或者文件路徑時,提供文件名或者文件路徑的自動補全
4 插件名稱:Guides , 插件功能: 這個插件的作用是增加 .py
中的指示線,用這個插件能讓代碼的層次結構更加清晰。
5 插件名稱: Bracket Pair Colorizer , 插件功能: 這個插件的作用是給代碼中的括號增加顏色,同一對括號是相同的顏色,尤其是在括號中還包着括號的時候,看起來更加的清晰。
6 插件名稱: topper , 插件功能: 這個插件的作用是在.py文件的開頭添加一些說明header
三 、 配置
1 創建python文件夾
vscode 是基於文件夾的編輯器,我們可以首先建立一個文件夾叫做python
,作為我們的Python編程工作空間,只要一次配置好了這個工作空間,以后這個工作空間的配置就會對它之下的所有的.py
文件都起作用。
打開vscode,點擊左上角文件
—> 打開文件夾
,然后打開剛剛建立的PYTHON
文件夾。新建一個 hello.py 文件
2 配置 launch.json 文件
點擊菜單欄調試
—> 打開配置
,就會彈出一個選擇框,我們在這里要選擇Python
,然后就打開了launch.json
文件:
我們看到的launch.json
文件中的內容如上圖所示。同時我們還發現,在python工作區PYTHON
下面還多了一個文件夾.vscode
, 而且launch.json
就在這個文件夾中。
對launch.json
文件的配置如下: 這是我配置的全部內容。可以參考一下
{ // 使用 IntelliSense 了解相關屬性。 // 懸停以查看現有屬性的描述。 // 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python3", "type": "python", "request": "launch", "stopOnEntry": false, "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37", //python3的安裝路徑 "program": "${file}", "cwd": "${workspaceFolder}", "env": {}, "envFile": "${workspaceFolder}/.env", "debugOptions": [ "RedirectOutput" ] }, { "name": "Python: Attach", "type": "python", "request": "attach", "localRoot": "${workspaceFolder}", "remoteRoot": "${workspaceFolder}", "port": 3000, "secret": "my_secret", "host": "localhost" }, { "name": "Python: Terminal (integrated)", "type": "python", "request": "launch", "stopOnEntry": false, "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37", "program": "${file}", "cwd": "", "console": "integratedTerminal", "env": {}, "envFile": "${workspaceFolder}/.env", "debugOptions": [] }, { "name": "Python: Terminal (external)", "type": "python", "request": "launch", "stopOnEntry": false, "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37", "program": "${file}", "cwd": "", "console": "externalTerminal", "env": {}, "envFile": "${workspaceFolder}/.env", "debugOptions": [] }, { "name": "Python: Django", "type": "python", "request": "launch", "stopOnEntry": true, "pythonPath": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37", "program": "${workspaceFolder}/manage.py", "cwd": "${workspaceFolder}", "args": [ "runserver", "--noreload", "--nothreading" ], "env": {}, "envFile": "${workspaceFolder}/.env", "debugOptions": [ "RedirectOutput", "Django" ] }, { "name": "Python: Flask (0.11.x or later)", "type": "python", "request": "launch", "module": "flask", "env": { "FLASK_APP": "app.py" }, "args": [ "run", "--no-debugger", "--no-reload" ] }, { "name": "Python: Module", "type": "python", "request": "launch", "module": "module.name" }, { "name": "Python: Pyramid", "type": "python", "request": "launch", "args": [ "${workspaceFolder}/development.ini" ], "debugOptions": [ "RedirectOutput", "Pyramid" ] }, { "name": "Python: Watson", "type": "python", "request": "launch", "program": "${workspaceFolder}/console.py", "args": [ "dev", "runserver", "--noreload=True" ] }, { "name": "Python: All debug Options", "type": "python", "request": "launch", "pythonPath": "${config:python.pythonPath}", "program": "${file}", "module": "module.name", "env": { "VAR1": "1", "VAR2": "2" }, "envFile": "${workspaceFolder}/.env", "args": [ "arg1", "arg2" ], "debugOptions": [ "RedirectOutput" ] } ] }
3 配置 tasks.json 文件
點擊菜單欄 終端 —> 配置任務
,就會彈出一個選擇框,我們在這里要選擇使用模板創建tasks.json文件
,然后又彈出一個選擇框,這里選擇Others
,就打開了tasks.json
文件:
配置修改如下:
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "python3", "type": "shell", "command": "C:/Users/Administrator/AppData/Local/Programs/Python/Python37", "args": [ "${file}" ] } ] }
4 用戶設置:
點擊菜單欄文件
—> 首選項
—> 設置
,然后打開用戶設置
:
在擴展里找到 python 打開,在 settings.json 編輯。
{ "window.zoomLevel": 1, "files.autoSave": "afterDelay", "explorer.confirmDragAndDrop": false,"editor.renderIndentGuides": false, "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, "python.linting.enabled": false, //啟用flake8,首先需要pip3 install falke8 "python.formatting.provider": "yapf", ///啟用yapf,首先需要pip3 install yapf "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", }
四 、 其他
1 需要更新一下pip
cmd 下 執行命令:
python -m pip install --upgrade pip
2 安裝 flake 8
cmd 下 執行命令:
pip install flake8
3 查看python 安裝路徑
>>> import sys >>> sys.path ['', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyth on37.zip', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37 \\DLLs', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\ lib', 'C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37', 'C :\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-p ackages']
最終結果: C:\Users\Administrator\AppData\Local\Programs\Python\Python37
配置到此結束,可以愉快玩耍了
快速調試。可以參看這篇文章: https://www.cnblogs.com/richerdyoung/p/12001508.html