Skip to main content
rule

isort — Cursor rule: what it does, how to install

Cursor rule that establishes Python import sorting conventions matching PEP 8 and the black profile.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/isort.mdc
Open on GitHub

What isort does

This rule guides Cursor whenever editing Python files covered by **/*.py. It mandates centralizing all import configuration inside pyproject.toml using the black profile and an 88-character line limit, explicitly rejecting standalone .isort.cfg files.

The instructions forbid wildcard imports like from module import * and enforce absolute import paths over relative ones. For modular repositories, it defines known_first_party and known_third_party arrays so internal modules group accurately.

If your repository also uses Ruff, sorting logic will clash unless you explicitly ignore rule I001 under [tool.ruff]. The rule also provides hook configurations for pre-commit and automated checks via isort --check-only . inside continuous integration pipelines.

What it helps with

  • eliminate merge conflicts across Python import blocks
  • standardize import styling with the black profile in pyproject.toml
  • separate internal packages from third-party libraries using known_first_party
  • replace wildcard imports with explicit declarations
  • prevent collisions between isort and Ruff rule I001

Example prompts for the agent

  • Configure the [tool.isort] section in pyproject.toml with the black profile
  • Refactor relative imports in models.py to absolute project paths
  • Add isort check-only validation to our continuous integration workflow

How to install isort

Manually: put the files in place

.cursor/rules/isort.mdc

Frontmatter: description, globs, alwaysApply

Step by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules

Questions about isort

How to install isort in Cursor?

Create the .cursor/rules/isort.mdc file in your repository root and add the rule text. Cursor reads it automatically for every file matching **/*.py.

What does the isort rule enforce?

It instructs Cursor to follow PEP 8 import conventions, use absolute imports, ban wildcard syntax, and centralize settings in pyproject.toml.

How to prevent conflicts between isort and Ruff?

Add ignore = ["I001"] to the [tool.ruff] section in pyproject.toml. This turns off Ruff's native import sorting so isort handles the task alone.

Source text

isort.mdc · CC0-1.0
---
description: This guide provides definitive rules for configuring and using isort to maintain consistent, readable, and merge-conflict-free Python import statements.
globs: **/*.py
---
# isort Best Practices

`isort` is the definitive tool for sorting Python imports. It ensures consistency, reduces merge conflicts, and improves readability by automatically organizing imports according to PEP 8 and project-specific rules.

## 1. Core Configuration: `pyproject.toml` is Non-Negotiable

Always configure `isort` in `pyproject.toml`. This centralizes tooling configuration and ensures consistent behavior across all development environments.

**❌ BAD: Scattered or Missing Configuration**
Relying on command-line flags or `.isort.cfg` files leads to inconsistency and makes project setup brittle.

**✅ GOOD: Centralized `pyproject.toml`**
Use the `black` profile and explicitly define your project's line length.

```toml
# pyproject.toml
[tool.isort]
profile = "black"
line_length = 88
# Add any top-level internal packages to known_first_party
# This ensures isort correctly groups your internal modules.
known_first_party = ["my_project_core", "my_project_api"]
# Add third-party packages that isort might misclassify as first-party
known_third_party = ["flask", "django", "requests"]
```

## 2. Enforce with `pre-commit` Hooks

Automate `isort` execution on every commit. This is the only way to guarantee consistent import ordering across the team.

**❌ BAD: Manual `isort` Runs**
Forgetting to run `isort` manually leads to unformatted code in the repository and wasted time in code reviews.

**✅ GOOD: `pre-commit` Integration**
Add `isort` to your `.pre-commit-config.yaml`.

```yaml
# .pre-commit-config.yaml
- repo: https://github.com/PyCQA/isort
  rev: 5.13.2 # Use the latest stable version
  hooks:
    - id: isort
      args: ["--profile", "black", "--line-length", "88"]
```

## 3. Code Organization and Import Grouping

`isort` excels at grouping imports. Leverage `known_first_party` to correctly categorize your internal modules.

**❌ BAD: Ambiguous Internal Imports**
Without `known_first_party`, `isort` might treat your internal modules (e.g., `from my_project.utils import ...`) as third-party or local, leading to incorrect grouping.

**✅ GOOD: Explicit `known_first_party` for Modular Projects**
If your project has a `src` directory or multiple top-level packages, define them.

```python
# my_project_core/logic.py
import os # Standard library
import requests # Third-party
from my_project_api.models import User # First-party (correctly grouped)

# ... rest of your code
```

## 4. Avoid Wildcard Imports

Wildcard imports (`from module import *`) obscure dependencies and make code harder to read and refactor. `isort` will sort them, but they are a fundamental anti-pattern.

**❌ BAD: Wildcard Imports**
```python
from my_module import * # What exactly is imported?
```

**✅ GOOD: Explicit Imports**
```python
from my_module import specific_function, AnotherClass
```

## 5. Relative vs. Absolute Imports

Use absolute imports for clarity and maintainability. Relative imports are acceptable *within* a subpackage but should be used sparingly.

**❌ BAD: Over-reliance on Relative Imports**
```python
# In my_package/sub_package/module_a.py
from ..another_sub_package.module_b import func # Hard to trace origin
```

**✅ GOOD: Absolute Imports for Top-Level Modules**
```python
# In my_package/sub_package/module_a.py
from my_project.another_sub_package.module_b import func # Clear path
```

## 6. Performance and CI/CD Integration

`isort` is fast. Integrate it into your CI/CD pipeline as a check-only step to prevent unformatted code from being merged.

**❌ BAD: No CI/CD Check**
Allowing unformatted code to pass CI means `pre-commit` is the only gate, which can be bypassed.

**✅ GOOD: CI/CD `isort --check-only`**
Run `isort --check-only .` in your CI pipeline. This fails the build if imports are not sorted, forcing developers to fix them locally.

## 7. Ruff and `isort` Coexistence

While Ruff can replace `isort`, many teams still use `isort` for its dedicated import handling. If you're using both, ensure Ruff's import sorting is disabled to avoid conflicts.

**✅ GOOD: Disable Ruff's Import Sorting if Using `isort`**
```toml
# pyproject.toml (for Ruff)
[tool.ruff]
# Disable isort-related rules (e.g., I001 for unsorted imports)
ignore = ["I001"]
```

© sanjeed5 · CC0-1.0 · sanjeed5/awesome-cursor-rules-mdc

Similar IDE rules

All IDE rules for Code review and refactoring →

From the same set240

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor