Format Python code with Black, the uncompromising code formatter. Automatically applies PEP 8 compliant opinionated formatting for consistent, readable code.
Automatically format Python code using Black, the uncompromising code formatter that enforces consistent style across your entire codebase.
This skill formats Python code files or directories using Black (v26.1.0+), applying PEP 8 compliant formatting with sensible defaults. Black removes manual formatting decisions, ensuring code looks identical across all projects and enabling faster code reviews through minimal diffs.
When the user requests Python code formatting:
1. **Verify Black Installation**
- Check if Black is installed: `black --version`
- If not installed, install with: `pip install black`
- For Jupyter notebook support: `pip install "black[jupyter]"`
2. **Identify Target Files**
- Ask the user which file(s) or directory to format
- Use the Read tool to examine the current code structure
- Check for existing `pyproject.toml` configuration
3. **Apply Formatting**
- For a single file: `black {filename.py}`
- For a directory: `black {directory_path}`
- For the entire project: `black .`
- If running as a script fails, try: `python -m black {target}`
4. **Handle Configuration (Optional)**
- Check if `pyproject.toml` exists in the project root
- If the user wants custom configuration, create or update `pyproject.toml`:
```toml
[tool.black]
line-length = 88
target-version = ['py310']
include = '\.pyi?$'
```
- Common options: `line-length`, `target-version`, `include`, `exclude`
5. **Verify Results**
- Black will display which files were reformatted
- Use the Read tool to show the user a sample of formatted code if requested
- Confirm that formatting completed successfully
6. **Add to Project Workflow (Optional)**
- If the user wants to enforce Black formatting:
- Suggest adding a pre-commit hook
- Recommend adding `black --check .` to CI/CD pipeline
- Provide badge for README: `[](https://github.com/psf/black)`
**Example 1: Format a single file**
```bash
black main.py
```
**Example 2: Format entire src directory**
```bash
black src/
```
**Example 3: Check formatting without modifying files**
```bash
black --check .
```
**Example 4: Format with fast mode (skip AST safety checks)**
```bash
black --fast .
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/black-code-formatter/raw