Linux command
yapf 命令
文件
复制后可按需替换文件名、目录或参数。
常用示例
Format a Python file
yapf [file.py]
Format a file in place
yapf -i [file.py]
Show diff
yapf -d [file.py]
Format using Google style
yapf --style=google [file.py]
Format all Python files
yapf -r -i [directory/]
Format in parallel
yapf -p -r -i [directory/]
Show available style options
yapf --style-help
Use custom style configuration
yapf --style='{based_on_style: pep8, indent_width: 4}' [file.py]
说明
yapf (Yet Another Python Formatter) is a Python code formatter developed by Google. Unlike linters that only warn about style violations, yapf reformats code to match the configured style, producing consistent output regardless of the original formatting. The algorithm analyzes code structure and calculates optimal formatting based on the configured style. Built-in styles include pep8 (default), google, chromium, and facebook. Custom styles can be defined by inheriting from a base style and overriding specific options. Configuration files (.style.yapf, setup.cfg, or pyproject.toml) in the source directory or parent directories are automatically detected. The config file uses INI format with a style section. yapf can format specific line ranges with --lines, useful for formatting only changed code in version control workflows.
参数
- -i, --in-place
- Modify files in place
- -d, --diff
- Print diff of changes without modifying files
- -r, --recursive
- Recursively format files in directories
- -p, --parallel
- Run formatting in parallel for multiple files
- -s, --style _style_
- Style: pep8, google, chromium, facebook, or path to config file
- --style-help
- Show documentation for all style options
- -l, --lines _range_
- Format only specified lines (e.g., 1-10,15-20)
- -e, --exclude _pattern_
- Exclude files matching pattern
- --version
- Show version and exit
FAQ
What is the yapf command used for?
yapf (Yet Another Python Formatter) is a Python code formatter developed by Google. Unlike linters that only warn about style violations, yapf reformats code to match the configured style, producing consistent output regardless of the original formatting. The algorithm analyzes code structure and calculates optimal formatting based on the configured style. Built-in styles include pep8 (default), google, chromium, and facebook. Custom styles can be defined by inheriting from a base style and overriding specific options. Configuration files (.style.yapf, setup.cfg, or pyproject.toml) in the source directory or parent directories are automatically detected. The config file uses INI format with a style section. yapf can format specific line ranges with --lines, useful for formatting only changed code in version control workflows.
How do I run a basic yapf example?
Run `yapf [file.py]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -i, --in-place do in yapf?
Modify files in place