yyhhyy's blog

yyhhyy

VS Code/Cursor 配置Ruff

37
2025-02-11

1. 为什么选择Ruff

引用官方原话: An extremely fast Python linter and code formatter, written in Rust.

一个用Rust编写的极速Python代码检查器格式化工具

官方的一张测试图:

因此选择Ruff

2. VS Code/Cursor 具体配置

2.1 setting.json

快捷键: Ctrl+Shift+P

键入: setting.json

打开用户设置

2.2 配置

在适当的位置加入下方代码

    // jupyter配置
    "notebook.formatOnSave.enabled": true,
    "notebook.codeActionsOnSave": {
        "notebook.source.fixAll": "explicit",
        "notebook.source.organizeImports": "explicit"
    },
    // Python配置
    "[python]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.fixAll": "explicit",
            "source.organizeImports": "explicit"
        },
        "editor.defaultFormatter": "charliermarsh.ruff"
    },

这样配置后即可通过 Ctrl+S 保存文件的同时,自动进行格式化代码的操作~