Cursor Rules for Python: 10 .mdc Rules and a Rule Generator
Cursor rules for Python: 10 .mdc project rules for typing, ruff, pytest, web app layers, and migrations, plus a prompt that builds a set from your repo.

Cursor rules for Python are short .mdc files in .cursor/rules/ that tell Cursor's Agent how your Python repo works: which version and package manager you use, where code and tests live, and which commands to run. Scope each file with globs so test rules load only for test files. Below are 10 rule blocks you can paste and edit, plus a flagship prompt that writes a full rule set from your repo facts.
A quick note on format. Project rules must use the .mdc extension; a plain .md file in .cursor/rules/ is ignored. The single .cursorrules file in the repo root is the legacy format, so use .mdc files instead. The rule types in Cursor are Always Apply, Apply Intelligently, Apply to Specific Files, and Apply Manually. Cursor's docs suggest keeping rules under 500 lines and pointing to your formatter instead of copying a style guide.
Rules for the whole repo
1. Toolchain rule (Always Apply)
---
alwaysApply: true
---
- Python [version]. Dependencies are managed with [uv, Poetry, or pip-tools]; never suggest another installer.
- Commands: [test command], [lint command], [format command], [type check command].
2. Never-do rule
- Never edit [generated or protected paths].
- Ask before adding a dependency.
- Never call [real external service] from code or tests.
3. Style points to the formatter
- Formatting and lint come from [ruff or black plus flake8] with line length [number]. Run [format command] instead of hand-formatting. Do not restate style rules the formatter enforces.
Rules for source files (Apply to Specific Files)
4. Typing
---
globs: [src folder]/**/*.py
alwaysApply: false
---
- Type every function signature. [mypy or pyright] runs in [mode] on [folders].
- Prefer built-in generics (list[str]) and [your Optional style].
5. Layering for web apps
- Keep [routers or views] thin: validate input with [Pydantic or forms], call a function in [services folder], return a response model.
- No database queries in [routers or views].
6. Errors and logging
- Raise specific exceptions in services and convert them to HTTP errors only at the edge.
- Use [logger setup]; never print in library code.
7. Scripts folder
- Scripts in [scripts folder] take arguments with [argparse or typer], have a dry run flag, and never write to production without [confirmation step].
Rules for tests and risky areas
8. Tests (globs on the test folder)
---
globs: tests/**/*.py
alwaysApply: false
---
- Use [pytest] and fixtures in [conftest path].
- Fake [external services] with [respx, responses, or your fake]; tests never make real network calls.
9. Migrations (Apply Intelligently)
---
description: Rules for database migrations in [migrations folder]
alwaysApply: false
---
- Create a new revision with [command]; never edit an existing migration.
10. Mistake log rule
- [Mistake the Agent made, written as a direct rule, for example "Never put SQL in a router"].
- [Next mistake as a rule].
Generate a full set from your repo
The Cursor Rules for Python: .mdc Rule Set From Your Repo Facts prompt takes your toolchain, folder layout, framework patterns, test setup, real commands, never-do list, and the mistakes the Agent already made. It returns a file plan with rule types, four .mdc files, a check list that traces every command and path back to your inputs, and two test prompts. Its example is a FastAPI service with uv, ruff, strict mypy, Alembic, and pytest with respx.
Test the rules
After saving the files, open a source file and ask the Agent for a small change. If it still suggests the wrong installer or puts queries in the wrong layer, check the rule type and globs first, then add one direct line to the mistake log rule. Commit the rules so the whole team gets them.
Working in another stack too? See Cursor Rules for React and Cursor Rules for Java Spring Boot, and for mobile apps, Cursor Rules for Flutter. For Python testing and debugging, use the pytest fixture design prompt and the Python traceback debugger.
Find more agent setup prompts in the Agents category, look through Browse, or post your own rules on Share.