【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;

(完)

发布了66 篇原创文章 · 获赞 17 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/DovSnier/article/details/103578935