Skip to main content
rule

python — Cursor rule: Python, Flask and SQLite conventions

A Cursor rule that sets project layout, code style and stack choices for Python work with Flask and SQLite. Useful when the agent edits files in your repo.

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

What python does

This is a Cursor rules file that lives at .cursor/rules/python.mdc. It activates on **/*.py, src/**/*.py, tests/**/*.py, so the guidance shows up when the agent touches Python code. Auto-apply is off: the rule loads on file match instead of sitting in context all the time.

Inside is a set of conventions. Layout: src/your_package_name/, tests in tests/ next to src/, config in config/ or environment variables, dependencies in requirements.txt or pyproject.toml, Jinja2 templates in templates/. Style: Black formatting, isort for imports, snake_case and PascalCase naming, 88-character lines, absolute imports. Separate sections cover type hints, the Flask factory pattern with Blueprints, SQLAlchemy with Alembic migrations, auth via Flask-Login and bcrypt, REST with Flask-RESTful, pytest, security and performance.

The rule is tied to that stack: Flask, SQLAlchemy, Alembic, Flask-Login, Flask-RESTful, pytest. On FastAPI, Django or a different ORM, a good chunk of it will argue with your existing code, and you will end up rewriting the file. It is plain markdown, so edits are manual.

What it helps with

  • set up src-layout and a tests folder from the first commit
  • keep Black, isort and the 88-character limit in place
  • split Flask routes into Blueprints instead of one large file
  • wire SQLAlchemy with Alembic migrations
  • handle auth with Flask-Login and bcrypt

Example prompts for the agent

  • Set up the project structure: src-layout, tests next to src, config in config/
  • Split the Flask routes into Blueprints and move models into separate modules
  • Configure pytest with fixtures and cover every route with tests

How to install python

Manually: put the files in place

.cursor/rules/python.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 python

How do I install the python rule in Cursor?

Create .cursor/rules/python.mdc in your project root and paste the rule contents into it. Cursor picks it up through the **/*.py, src/**/*.py, tests/**/*.py glob.

What does the python rule do?

It gives the agent conventions for a Python project: folder layout, Black and isort formatting, type hints, the Flask factory pattern with Blueprints, SQLAlchemy and Alembic, and pytest tests.

Does it work for FastAPI or Django?

No. The Flask, Flask-Login and Flask-RESTful sections target that framework, so you would need to rewrite the file for another stack.

Source text

python.mdc · CC0-1.0
---
description: "Python best practices and patterns for modern software development with Flask and SQLite"
globs: **/*.py, src/**/*.py, tests/**/*.py
alwaysApply: false
---
# Python Best Practices

## Project Structure
- Use src-layout with `src/your_package_name/`
- Place tests in `tests/` directory parallel to `src/`
- Keep configuration in `config/` or as environment variables
- Store requirements in `requirements.txt` or `pyproject.toml`
- Place static files in `static/` directory
- Use `templates/` for Jinja2 templates

## Code Style
- Follow Black code formatting
- Use isort for import sorting
- Follow PEP 8 naming conventions:
  - snake_case for functions and variables
  - PascalCase for classes
  - UPPER_CASE for constants
- Maximum line length of 88 characters (Black default)
- Use absolute imports over relative imports

## Type Hints
- Use type hints for all function parameters and returns
- Import types from `typing` module
- Use `Optional[Type]` instead of `Type | None`
- Use `TypeVar` for generic types
- Define custom types in `types.py`
- Use `Protocol` for duck typing

## Flask Structure
- Use Flask factory pattern
- Organize routes using Blueprints
- Use Flask-SQLAlchemy for database
- Implement proper error handlers
- Use Flask-Login for authentication
- Structure views with proper separation of concerns

## Database
- Use SQLAlchemy ORM
- Implement database migrations with Alembic
- Use proper connection pooling
- Define models in separate modules
- Implement proper relationships
- Use proper indexing strategies

## Authentication
- Use Flask-Login for session management
- Implement Google OAuth using Flask-OAuth
- Hash passwords with bcrypt
- Use proper session security
- Implement CSRF protection
- Use proper role-based access control

## API Design
- Use Flask-RESTful for REST APIs
- Implement proper request validation
- Use proper HTTP status codes
- Handle errors consistently
- Use proper response formats
- Implement proper rate limiting

## Testing
- Use pytest for testing
- Write tests for all routes
- Use pytest-cov for coverage
- Implement proper fixtures
- Use proper mocking with pytest-mock
- Test all error scenarios

## Security
- Use HTTPS in production
- Implement proper CORS
- Sanitize all user inputs
- Use proper session configuration
- Implement proper logging
- Follow OWASP guidelines

## Performance
- Use proper caching with Flask-Caching
- Implement database query optimization
- Use proper connection pooling
- Implement proper pagination
- Use background tasks for heavy operations
- Monitor application performance

## Error Handling
- Create custom exception classes
- Use proper try-except blocks
- Implement proper logging
- Return proper error responses
- Handle edge cases properly
- Use proper error messages

## Documentation
- Use Google-style docstrings
- Document all public APIs
- Keep README.md updated
- Use proper inline comments
- Generate API documentation
- Document environment setup

## Development Workflow
- Use virtual environments (venv)
- Implement pre-commit hooks
- Use proper Git workflow
- Follow semantic versioning
- Use proper CI/CD practices
- Implement proper logging

## Dependencies
- Pin dependency versions
- Use requirements.txt for production
- Separate dev dependencies
- Use proper package versions
- Regularly update dependencies
- Check for security vulnerabilities

© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules

Similar IDE rules

All IDE rules for Programming languages →

From the same set68

react-query-cursorrules-prompt-file A Cursor rule that tells the agent how to write React Query code: where QueryClient lives, how hooks and query keys are named. ★ 41,011repo stars react-native-expo-cursorrules-prompt-file A Cursor rule that tells the agent how to write React Native code with Expo. Useful when you want new screens and components to follow one stack. ★ 41,011repo stars rails-cursorrules-prompt-file A Cursor rule file that tells the agent how to write Rails 8 code: generators, bin/dev, the Solid stack, Minitest and Hotwire. ★ 41,011repo stars qwik-basic-cursorrules-prompt-file A Cursor rule that sets Qwik conventions for the agent: lazy functions with the $ suffix, signals, tasks, and a TypeScript plus Vite project layout. ★ 41,011repo stars python-github-setup-cursorrules-prompt-file Tells Cursor how to write and review Python code: style, tests, GitHub Pull Requests and GitHub Actions CI. Useful when the team has no shared rulebook. ★ 41,011repo stars python-fastapi-best-practices-cursorrules-prompt-f A Cursor rule that sets the agent's coding style for Python and FastAPI: functional code, Pydantic v2, async operations. ★ 41,011repo stars python-cursorrules-prompt-file-best-practices A Cursor rule that makes the assistant write Python with type annotations, PEP 257 docstrings and pytest tests. ★ 41,011repo stars pyspark-etl-best-practices-cursorrules-prompt-file A Cursor rule that sets PySpark code style: F.col(), explicit joins, select over withColumn, and Iceberg-aware ETL structure. ★ 41,011repo stars

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