VSCode中怎么配置Python,實現錯誤提醒和自動格式化

本篇文章帶大家了解一下vscode中配置python的方法,實現寫python時的代碼錯誤提醒和自動格式化功能。

VSCode中怎么配置Python,實現錯誤提醒和自動格式化

Come on!Python 代碼錯誤檢查:

Python 的代碼錯誤檢查通常用 pep8、pylint和flake8,自動格式化代碼通常用 autopep8、yapf、black。這些工具均可以利用pip進行安裝,這里介紹傳統的利用 pip.exe 安裝和在 vscode 中安裝兩種方式。【推薦學習:《vscode教程》】

如果要使用 flake8 或要想 flake8 等工具起作用,前提是必須把 settings.json 文件中的”python.linting.enabled”值設為“true”,否則即使安裝了這些工具,也起不到代碼的錯誤提醒。

//?python相關配置 ??"python.linting.flake8Enabled":?true, ??"python.formatting.provider":?"yapf", ??"python.linting.flake8Args":?[ ????"--max--line-length=248" ??], ??"python.linting.pylintEnabled":?false, ??"workbench.startupEditor":?"newUntitledFile", ??"python.pythonPath":?"C:UserslenovoAppDataLocalProgramsPythonPython37python.exe", ??"python.linting.enabled":?true, ??"editor.fontSize":?14,? ??//?截止python相關配置

把上述配置放入 settings.json 中

VSCode中怎么配置Python,實現錯誤提醒和自動格式化

立即學習Python免費學習筆記(深入)”;


【傳統安裝方式】

以安裝為例 flake8,其余類似

方法一:

  • 打開命令行窗口(同時按 Win+R,輸入cmd )

  • 輸入:python -m pip install flake8,回車運行等待結果即可

【注意】前提是必須將python的路徑添加至環境變量。

方法二:

  • 打開命令行窗口(同時按 Win+R,輸入cmd)

  • 將 pip.exe 直接拖進cmd窗口

  • 輸入:pip install flake8,回車等待運行結果即可

方法三:

  • 在 pip.exe 所在的目錄,按 Shift+鼠標右鍵 ,打開 PowerShell窗口(win10 以前的版本是可以直接打開命令行窗口)

  • 輸入:cmd,回車運行

  • 輸入:pip install flake8,回車等待運行結果即可


【在 Vscode 中安裝】**

代碼錯誤工具以 flake8 為例:

1、在 VScode 中打開設置,搜索 python.linting.flake8enabled

2、在 Settings 界面中勾選

  • Whether to lint Python files using flake8

VSCode中怎么配置Python,實現錯誤提醒和自動格式化

【在Vscode中安裝格式化】VsCode 當你按下格式化快捷鍵 “Alt+Shift+F” 時,右下角會提示安裝 autopep8自動格式化工具,點擊yes等待安裝完畢再格式化即可。


到這里:VsCode寫Python時的代碼錯誤提醒和自動格式化!分享完畢了,快去試試吧!

更多編程相關知識,請訪問:vscode!!

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享