【Python】【VS Code】VS Code中python.json和setting.json文件配置说明

目录

1. python.json配置

2. setting.json配置

3. 解决中文乱码

4. 实现效果


1. python.json配置

           python.json 获取步骤:文件 -> 首选项 -> 配置用户代码片段 -> python

           此为VS Code的头文件设置,复制以下内容到 python.json

{
    "HEADER":{
    "prefix": "pyheader",
    "body": [   
    "#!/usr/bin/env python3",
    "# _*_ coding : UTF-8 _*_",
    "# 开发团队 :ALL",
    "# 开发人员 :Admin",
    "# 开发时间 :$CURRENT_YEAR/$CURRENT_MONTH/$CURRENT_DATE $CURRENT_HOUR:$CURRENT_MINUTE:$CURRENT_SECOND",
    "# 文件名称 :$TM_FILENAME",
    "# 开发工具 :Visual Studio Code",
    "",
    "",
    "",
    "if __name__ == '__main__':",
    "$0"
    ],
    }
}

用法:编辑一个.py文件,输入pyheader,就可以补全以下内容啦

#!/usr/bin/env python3
# _*_ coding : UTF-8 _*_
# 开发团队 :ST
# 开发人员 :Jly
# 开发时间 :2024/02/19 19:30:06
# 文件名称 :code_statistics.py
# 开发工具 :Visual Studio Code



if __name__ == '__main__':

2. setting.json配置

        setting.json获取步骤:文件 -> 首选项 -> 设置

        此为VS Code的头文件设置,涉及到环境配置、代码颜色、行间距、字体粗细、中文环境配置等等

{
    "security.workspace.trust.untrustedFiles": "open",
    "workbench.colorTheme": "Oceanic Next (dimmed bg)",
    "workbench.iconTheme": "material-icon-theme",
    "bracket-pair-colorizer-2.depreciation-notice": false,
    "editor.lineHeight": 24,      //控制行间距
    "editor.fontWeight": "normal",    // 控制字体粗细
    "editor.tokenColorCustomizations": {"comments": "#00ff7f",
    "keywords": "#eb6689",
    "numbers": "#00eeff",
    "functions": "#ea65f9"
    },  //控制字体颜色
    "python.defaultInterpreterPath": "D:\\soft\\Anaconda3\\python.exe",
    //"python.linting.flake8Enabled": true,
    //"python.formatting.provider": "yapf",
    //"python.linting.flake8Args": ["--max-line-length=248"],
    //"python.linting.pylintEnabled": false,
    "python.autoComplete.extraPaths": ["D:/soft/Anaconda3/lib/site-packages",],
    "python.analysis.extraPaths": ["D:/soft/Anaconda3",],
    //"vsicons.dontShowNewVersionMessage": true,
    // 配置python语言正常输出中文的环境
    "code-runner.executorMap": {
    "python":"set PYTHONIOENCODING=utf8 && $pythonPath -u $fullFileName"}, 
    "code-runner.respectShebang": false,
    "editor.unicodeHighlight.includeStrings": false,
    "editor.unicodeHighlight.includeComments": false,
//终端颜色配置
    "terminal.integrated.inheritEnv": false,
    "workbench.colorCustomizations": {
        "terminal.background": "#200707",
        "terminal.foreground": "#b4d6af",
        "terminalCursor.background": "#D8D8D8",
        "terminalCursor.foreground": "#D8D8D8",
        "terminal.ansiBlack": "#181818",
        "terminal.ansiBlue": "#0b5e7c",
        "terminal.ansiBrightBlack": "#585858",
 
// 终端命令行  lm@os:
        "terminal.ansiBrightBlue": "#39b9e7",
        "terminal.ansiBrightCyan": "#86C1B9",
        "terminal.ansiBrightGreen": "#ca530f",
        "terminal.ansiBrightMagenta": "#BA8BAF",
        "terminal.ansiBrightRed": "#AB4642",
        "terminal.ansiBrightWhite": "#F8F8F8",
        "terminal.ansiBrightYellow": "#f788f7",
        "terminal.ansiCyan": "#86C1B9",
        "terminal.ansiGreen": "#b57b6c",
        "terminal.ansiMagenta": "#BA8BAF",
        "terminal.ansiRed": "#AB4642",
        "terminal.ansiWhite": "#D8D8D8",
        "terminal.ansiYellow": "#F7CA88",
        "terminal.integrated.cursorBlinking": true,
        "terminal.integrated.lineHeight": 1.6,
        "terminal.integrated.letterSpacing": 0.1,
        "terminal.integrated.fontFamily": "Lucida Console", //字体设置
        "terminal.integrated.shell.linux": "/bin/zsh",
    },
    "[python]": {
        "editor.formatOnType": true
    },
    "vsicons.dontShowNewVersionMessage": true,
    "settingsSync.ignoredExtensions": [
    
    ],
    "settingsSync.ignoredSettings": [

    ],
    "window.zoomLevel": -1,
    "editor.codeActionsOnSave": {
    
    }
}

3. 解决中文乱码

        在setting.json文件里面增加以下内容即可

// 配置python语言正常输出中文的环境
"code-runner.executorMap": {
    "python":"set PYTHONIOENCODING=utf8 && $pythonPath -u $fullFileName"}, 
"code-runner.respectShebang": false
},

4. 实现效果

### 回答1: 可以在 Visual Studio Code 的设置文件 (settings.json) 加入以下配置: ``` "python.formatting.operator_spacing": true ``` 这样,在使用代码格式化命令时,VSCode 就会自动在运算符前后加上空格。 注意:需要使用 Python 插件,否则这个配置不会生效。 ### 回答2: 在VSCode,可以通过设置`Editor: Format On Save`选项来实现保存时自动格式化代码。要在运算符前后自动加上空格,可以通过在`setting.json`文件配置`python.formatting.provider`属性来指定代码格式化程序。以下是相应的配置步骤: 1. 打开VSCode,进入“文件”菜单并选择“首选项”下的“设置”。 2. 在用户设置,可以搜索“python.formatting.provider”以找到代码格式化程序。 3. 在`setting.json`文件,添加以下配置代码: ```json "python.formatting.provider": "autopep8", "python.formatting.autopep8Args": [ "--indent-size=4", "--aggressive", "--ignore=E501,E303" ], ``` 以上配置使用了`autopep8`作为代码格式化程序,它可以自动调整代码格式。`--indent-size=4`指定缩进大小为4个空格,`--aggressive`使用比较严格的模式进行格式化,`--ignore=E501,E303`忽略一些特定的格式化错误。 配置完上述内容后,当你保存Python代码时,VSCode会自动调用`autopep8`对代码进行格式化,包括在运算符前后加上空格。例如,将`a=b c`格式化为`a = b c`。 请注意,为了使该配置生效,你需要先安装`autopep8`格式化工具。可以通过在终端运行以下命令来安装: ``` pip install autopep8 ``` 这些步骤可以实现在VSCode使用`autopep8`自动格式化Python代码,并在运算符前后加上空格。 ### 回答3: 要在VS Code自动格式化Python代码时,在运算符(- * / 等)前后加上空格,你可以通过修改setting.json文件进行配置。 首先,打开VS Code并进入菜单栏选择"文件" > "首选项" > "设置"。 接下来,你可以在"搜索设置"框输入"Python Formatting"来查找与Python代码格式相关的设置。 找到"Python Formatting: Provider"选项,选择"autopep8",它是一种常用的Python代码格式化工具。 然后,找到"Python Formatting: Args"选项,点击编辑设置。 在弹出的编辑器,你可以输入以下内容: ```json { "python.formatting.autopep8Args": [ "--indent-size", "4" ], "python.formatting.provider": "autopep8", "python.formatting.autopep8Path": "autopep8" } ``` 这段代码指示了将使用autopep8进行代码格式化,并设置了缩进大小为4个空格。 保存设置后,当你在VS Code使用快捷键Ctrl + Shift + I来格式化Python代码时,- * /等运算符前后会自动加上空格,从而达到你的预期效果。 希望以上内容对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风云说通信

支持小编为您创造更多干货,谢谢

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值