Vscode for python ide配置

1.文件头添加

  • 自定义代码片段
  1. 文件>首选项>用户代码片段
  2. 搜索python
  3. 添加代码
"HEADER":{
        "prefix": "header",
        "body": [
            "# -*- encoding: utf-8 -*-",
            "",
            "# @File    : $TM_FILENAME",
            "# @Time    : $CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
            "# @Author  : H2o ",
            "# @Version : 1.0",
            "# @Contact : [email protected]",
            "",
            "",
            ""
        ],
    }
  • 添加文件头
  1. 新建文件
  2. 输入header
  3. 选择header

2.进入设置界面

  1. 在vscode中打开一个文件
  2. 点击左下角"齿轮"图标
  3. 选中设置

3.为不同的项目配置python虚拟环境

  1. 设置界面, 切换tab到工作区
  2. 设置搜索框输入:python.pythonPath
  3. 将新建的虚拟环境地址添加即可, 如: D:\ENV\Testtools\Scripts\python.exe

4.自动函数生成注释模板

  1. 下载插件: autoDocstring
  2. 快捷键: ctrl+shift+2, 或在函数定义后输入: """

    4.1.切换注释生成模板

  3. 设置界面, 搜索autoDocstring
  4. 修改autoDocstring.docstringFormat栏即可

    4.2.自定义模板样式

  5. 新建.mustache文件
  6. 修改autoDocstring.docstringFormat栏为default
  7. 修改autoDocstring.customTemplatePath为你新建的.mustache文件的地址

(详细关键字配置请查看插件介绍)

个人使用google注释模板,.mustache文件内容如下:

{{! Google Docstring Template }}
{{summaryPlaceholder}}
{{extendedSummaryPlaceholder}}
{{#parametersExist}}
Args:
{{#args}}
    {{var}} ({{typePlaceholder}}): {{descriptionPlaceholder}}
{{/args}}
{{#kwargs}}
    {{var}} ({{typePlaceholder}}, optional): {{descriptionPlaceholder}}. Defaults to {{&default}}.
{{/kwargs}}
{{/parametersExist}}

{{#exceptionsExist}}
Raises:
{{#exceptions}}
    {{type}}: {{descriptionPlaceholder}}
{{/exceptions}}
{{/exceptionsExist}}

{{#returnsExist}}
Returns:
{{#returns}}
    {{typePlaceholder}}: {{descriptionPlaceholder}}
{{/returns}}
{{/returnsExist}}

5.配置代码格式化工具

(以black为例)

  1. pip install black
  2. 设置界面, 搜索python.formatting.provider, 选择black
  3. 设置界面, 搜索python.formatting.blackPath, 输入你的black安装路径
    例如: C:\Program Files\Python36\Scripts\black.exe
  4. (选配)配置black运行参数, 设置界面, 搜索python.formatting.blackArgs, 添加: --line-length=80

    (black每行允许的字符长度为88)

6.配置代码静态检查工具

(以pylint为例)

  1. pip install pylint (若是虚拟环境, 建议切换到虚拟环境后, 再pip安装)
  2. 设置界面, 搜索python.linting.pylintPath, 输入你的pylint安装的路径
  3. 设置界面, 搜索python.linting.enabled, 选中即可
  4. 设置界面, 搜索python.linting.pylintEnabled, 选中即可

7.插件安装

项目 说明 是否必需安装
Python
vscode-icons 美化文件图标
Better Comments 美化行注释
Chinese (Simplified) Language Pack
for Visual Studio Code
中文汉化包
autoDocstring 函数注释模板生成

8.json文件内容

8.1 用户json文件

{
    "files.autoSave": "afterDelay",
    "editor.renderWhitespace": "all",
    "editor.wordWrap": "wordWrapColumn",
    "workbench.startupEditor": "newUntitledFile",
    "workbench.iconTheme": "vscode-icons",
    "workbench.colorTheme": "Bluloco Light",
    "editor.fontFamily": "InputMono, Consolas, 'Courier New', monospace",
    "editor.fontSize": 15,
    "editor.lineHeight": 27,
    "python.formatting.provider": "black",
    "python.formatting.blackPath": "C:\\Program Files\\Python36\\Scripts\\black.exe",
    "python.formatting.blackArgs": [
        "--line-length=80"
    ],
    "editor.renderControlCharacters": false,
    "autoDocstring.customTemplatePath": "C:\\Program Files\\Microsoft VS Code\\data\\google.mustache"
}

8.2 工作区json文件

{
    "python.pythonPath": "D:\\ENV\\Testtools\\Scripts\\python.exe",
    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
}

猜你喜欢

转载自www.cnblogs.com/TesterH2o/p/11548086.html