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

 

 

 

 

(完)

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章