【VSCode】在VSCode 中格式化Python 代码

 

目录

1. 问题

2. 解决

3. 参考


 

1. 问题

最近在写python 的过程中,这个VSCode 每个月都会升级啥的,平常没咋注意,今天写了些python 发现有些格式不起作用;过去平常自己写的过程中也没咋觉得会有问题啊,突然就这样了,后来网上搜了搜研究了下;

 

Python 2.x 的不支持yapf 格式化,Python 3.x 才支持yapf 格式化;

今天我又试了一下,在2.7 版本下,我得是支持yapf 格式化的;

 

2. 解决

见如下代码块:

setting.json 配置项:

{
    // "python.pythonPath": "/usr/bin/python",
    "python.pythonPath": "venv/bin/python",
    "python.formatting.provider": "yapf",
    // "python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 4}"],
    // "python.linting.flake8Args": ["--max-line-length=250"],
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.detectIndentation": false,
    "files.insertFinalNewline": true
}

其实起作用的如下几行:

    "python.formatting.provider": "yapf",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "files.insertFinalNewline": true,

现在我自己的配置如下:

版本 一

{
    // "python.pythonPath": "/usr/bin/python",
    "python.pythonPath": "venv/bin/python",    // 3.x python 环境
    "python.formatting.provider": "yapf",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,

    "editor.formatOnSave": true,    // 针对editor
    "editor.tabSize": 4,
    "editor.detectIndentation": false,

    "files.insertFinalNewline": true    // 针对文件末尾添加新行 PEP8 规则
}

版本 二:

{
    "python.pythonPath": "venv2/bin/python",    // 2.x python 环境
    "python.formatting.provider": "yapf",
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.flake8Args": ["--max-line-length=250"],
    "editor.formatOnSave": true,
    "editor.tabSize": 4,
    "editor.detectIndentation": false,
    "workbench.editor.enablePreview": false,
    "workbench.editor.enablePreviewFromQuickOpen": false,
    "files.insertFinalNewline": true,
    "files.exclude": {
        "**/.git": true,
        "**/.svn": true,
        "**/.hg": true,
        "**/CVS": true,
        "**/.DS_Store": true,
        "**/*.pyc": true
    },

}

 

3. 参考

  1. Visual Studio Code中的Python
  2. 在Visual Studio Code中整理Python;
  3. pep8;

 

 

 

 

(完)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值