|
| 1 | +--- |
| 2 | +description: Enforce structured and absolute import order for Python files |
| 3 | +globs: |
| 4 | + - "**/*.py" |
| 5 | +alwaysApply: true |
| 6 | +--- |
| 7 | + |
| 8 | +Group, order, and format Python imports following these conventions: |
| 9 | + |
| 10 | +### ✅ Use **absolute imports only**: |
| 11 | +Always write imports using the full path from the project root. |
| 12 | +**Avoid** relative imports like `from .module import foo` or `from ..utils import bar`. |
| 13 | + |
| 14 | +- ✅ `from my_project.utils import foo` |
| 15 | +- ❌ `from .utils import foo` |
| 16 | +- ❌ `from ..submodule import bar` |
| 17 | + |
| 18 | +This ensures clarity, consistency across refactors, and better compatibility with tools like linters, IDEs, and packaging systems. |
| 19 | + |
| 20 | +--- |
| 21 | + |
| 22 | +### 📚 Import grouping and ordering: |
| 23 | + |
| 24 | +Organize imports into **three groups**, separated by a blank line: |
| 25 | + |
| 26 | +1. **Standard Library Imports** |
| 27 | + e.g., `import os`, `from datetime import datetime` |
| 28 | + |
| 29 | +2. **Third-Party Library Imports** |
| 30 | + e.g., `import numpy as np`, `from sqlalchemy import Column` |
| 31 | + |
| 32 | +3. **Local Application/Library Imports** |
| 33 | + e.g., `from my_project.utils import helper_function` |
| 34 | + |
| 35 | +--- |
| 36 | + |
| 37 | +### 🔤 Within each group: |
| 38 | + |
| 39 | +- First: `import module` (in alphabetical order) |
| 40 | +- Then: `from module import name` (in alphabetical order) |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +### ✅ Example: |
| 45 | + |
| 46 | +```python |
| 47 | +import os |
| 48 | +import sys |
| 49 | +from datetime import datetime |
| 50 | + |
| 51 | +import numpy as np |
| 52 | +import pandas as pd |
| 53 | +from sqlalchemy import Column, Integer |
| 54 | + |
| 55 | +import my_project |
| 56 | +from my_project.utils import helper_function |
0 commit comments