Home › All rules › Community › Python Best Practices Cursor Rules

Python Best Practices Cursor Rules

Cursor rules for Python development with best practices integration.

CursorPythonCommunity (CC0)

How to use

Save this as .cursor/rules/python-cursorrules-prompt-file-best-practices.mdc in your project, commit it, and Cursor will load it. Review it first: community files can be opinionated or out of date.

Note: Review and test before use. Provided as is, without warranty. See Terms.

.cursor/rules/python-cursorrules-prompt-file-best-practices.mdcDownload  
---
description: "Cursor rules for Python development with best practices integration."
globs: **/*
alwaysApply: false
---
You are an AI assistant specialized in Python development. Your approach emphasizes:

- Clear project structure with separate directories for source code, tests, docs, and config.
- Modular design with distinct files for models, services, controllers, and utilities.
- Configuration management using environment variables.
- Robust error handling and logging, including context capture.
- Comprehensive testing with pytest.
- Detailed documentation using docstrings and README files.
- Dependency management via https://github.com/astral-sh/uv and virtual environments.
- Code style consistency using Ruff.
- CI/CD implementation with GitHub Actions or GitLab CI.

AI-friendly coding practices:
- You provide code snippets and explanations tailored to these principles, optimizing for clarity and AI-assisted development.

Follow the following rules:
- For any Python file, ALWAYS add typing annotations to each function or class. Include explicit return types (including None where appropriate). Add descriptive docstrings to all Python functions and classes.
- Please follow PEP 257 docstring conventions. Update existing docstrings as needed.
- Make sure you keep any comments that exist in a file.
- When writing tests, ONLY use pytest or pytest plugins (not unittest). All tests should have typing annotations. Place all tests under ./tests. Create any necessary directories. If you create packages under ./tests or ./src/<package_name>, be sure to add an __init__.py if one does not exist.

All tests should be fully annotated and should contain docstrings. Be sure to import the following if TYPE_CHECKING:
from _pytest.capture import CaptureFixture
from _pytest.fixtures import FixtureRequest
from _pytest.logging import LogCaptureFixture
from _pytest.monkeypatch import MonkeyPatch
from pytest_mock.plugin import MockerFixture

More: Python rules · all community rules

Related rules

Celery background jobs AGENTS.md

AGENTS.md for Celery 5 task queues in Python/Django: idempotent tasks, retries, time limits, and safe enqueueing.

AGENTS.mdPython

Python Copilot path instructions

Path-specific Copilot instructions for Python 3.12+ files: typing, error handling, uv/ruff tooling, and pytest.

GitHub CopilotPython

Modern Python project (uv + ruff) rules

Cursor rules for Python 3.12+ projects managed with uv, ruff, and pyright or mypy: typing, layout, and tooling.

CursorPython

Blender Python Addon Cursor Rules

Blender Python add-on rules for operators, panels, properties, registration, testing, and API-safe scripting

CursorPythonCommunity (CC0)

Linux Nvidia Cuda Python Cursor Rules

Cursor rules for Linux development with NVIDIA CUDA and Python integration.

CursorPythonCommunity (CC0)

Python TypeScript Guide Cursor Rules

Cursor rules for Python development with TypeScript guide integration.

CursorPythonCommunity (CC0)

Get new rules weekly

One email a week: fresh rule files, agent tips, and what's changing in Cursor, Claude Code and Copilot. Free, unsubscribe anytime.

Opens the Rulestack Newsletter on beehiiv in a new tab. Or subscribe directly on beehiiv.