Invite a friend, get 1 month of Pro freeHow it works

Cursor Agent Python: Practical Guide for Python Projects

Learn to run Cursor Agent in a Python project, configure AGENTS.md and .cursor/rules, and build a test-driven loop with pytest, mypy, and ruff.

Solviera Teknoloji 6 min read

Start Cursor Agent from your project root with cursor-agent and it acts as a terminal-based coding partner that reads your Python project context. For Python work, the value comes from letting it run the real tools you already use—venv, pyproject.toml, mypy, pytest, and ruff—inside the same environment, not from treating it as a magic code generator. This guide shows a practical setup and a tight, verifiable loop for Python projects.

Why Cursor Agent fits Python work

Python codebases reward small, testable changes and punish broken imports, type drift, and environment mismatches. Cursor Agent operates in the terminal, so it can activate a virtualenv, run pytest -q, inspect tracebacks, and then edit src/ files directly. It does not need a GUI to manage refactors across multiple files; the agent reads and writes code like a developer pairing over the shell. Because it can call ruff and mypy, you can require it to verify each change with the same gates you use in CI. This closes the loop between an AI suggestion and a passing test suite, which is exactly what Python’s dynamic nature demands.

Project setup for the agent

Cursor Agent reads project instructions from AGENTS.md or .cursor/rules. For Python, keep these instructions command-oriented, not prose-heavy. The agent needs the exact commands that install, test, lint, and type-check the project. If the agent guesses, you get wrong environment or wrong Python version errors. Put the file at the repo root as AGENTS.md and keep it under about 80 lines.

A realistic example:

# AGENTS.md

## Commands
python -m venv .venv
source .venv/bin/activate
pip install -e '.[dev]'
pytest -q
ruff check .
ruff format --check .
mypy src

## Python conventions
- Python 3.12+; no `from __future__` except where required by a dependency.
- Use `pathlib` instead of `os.path`.
- Every public function must have type hints.
- Prefer dataclasses over dicts for simple records.
- Do not add third-party dependencies without updating `pyproject.toml` and the lock file.
- Keep import order: stdlib, third-party, local package.
- Before finishing a task, run `pytest -q`, `ruff check .`, and `mypy src`; fix all failures.

## Layout
- `src/yourpackage/` is the only importable package.
- `tests/` mirrors `src/yourpackage/`.
- No code in `__init__.py` except re-exports.

The exact commands come from your pyproject.toml; don’t document a command the project does not support. If you use uv or poetry, replace pip install -e with the appropriate equivalent, but the agent should see the real invocation. See How to use Cursor CLI: a step-by-step guide for the terminal workflow.

A working loop for Python changes

Use a short loop with the agent: describe one change, let it plan in two or three sentences, ask it to make the edit, run the test and lint commands, and then review git diff before the next step. Do not ask for a large feature in one prompt; a Python function with type hints, tests, and docstrings is already enough for one turn.

Example prompts:

  • Add a parse_toml function in src/config.py that returns a Config dataclass, then write two unit tests: one for valid input and one for malformed input.
  • Refactor src/report.py to replace all os.path usage with pathlib; run mypy src and ruff check . and fix the issues.
  • This test is flaky: test_retry_on_timeout sometimes fails. Inspect the fixture and the retry helper, propose a deterministic fix, and apply it.
  • Update the public API in src/__init__.py to export only Config, load_config, and save_config; adjust any internal imports and re-run the full test suite.

Each prompt should include the exact verification command when it matters. If the agent produces code, ask it to summarize the diff in one sentence and show the failing test before it fixes it. That keeps the loop honest.

Typical Python tasks and where to watch closely

Cursor Agent is strong at mechanical Python work: adding type hints, converting functions to async, writing pytest fixtures, renaming across modules, and updating import paths. It is also good at fixing common mypy errors when the fix is local, such as missing return annotations or incorrect Optional handling.

Watch closely in these areas:

  • Environment and dependency changes. The agent may edit pyproject.toml but forget to update the lock file or the constraints file. Always diff dependency files and re-run pip install -e '.[dev]' in a clean venv before trusting it.
  • Import side effects and circular imports. Python imports execute code. A seemingly harmless reorder in __init__.py can break application startup. Ask the agent to run a smoke import command like python -c 'import yourpackage' after such changes.
  • Type narrowing and third-party stubs. The agent may add # type: ignore to silence mypy instead of fixing the real type. Reject unexplained ignores; ask for the narrowest fix and run mypy on the changed files only.
  • Async and event loop behavior. If the agent converts synchronous code to async, it may create a new event loop inside a running loop or change retry semantics. Always test the actual runtime behavior, not just unit tests.
  • Mutable defaults and object identity. Classic Python bugs like def f(x=[]) can slip through. Ask the agent to scan for mutable default arguments in changed code.
TaskGood promptWhat to inspect
Add type hintsAdd complete type hints to src/api.py without changing behavior, then run mypy.git diff for Any and # type: ignore
Write testsWrite pytest tests for src/cache.py covering TTL expiry, missing key, and JSON decode errors.Fixture isolation and no network calls
Fix flaky testThe test test_upload_retries fails about 1 in 10 runs; make it deterministic and explain the root cause.No sleep calls; fake clock or injected retry delay
Refactor importsReplace os.path with pathlib across src/ and update all imports; run ruff and mypy.No os.path left; type errors on str vs Path

Testing and verification with Python

Treat the agent’s code as unverified until the tooling passes in a clean environment. Ask the agent to run pytest -q after every code change, and then run it yourself before merging. For Python, the automation loop should be:

  1. ruff check . and ruff format --check . to keep style deterministic.
  2. mypy src to catch type errors before tests run.
  3. pytest -q for behavior.
  4. python -c 'import yourpackage' after changing imports or packaging.

Do not let the agent fix tests by weakening assertions. If a test is outdated, require it to update the test and explain why the old expectation was wrong. When adding new code, ask for both happy path and failure path tests, especially for file parsing, API clients, and validation functions. Verify that new tests fail on the old code once, then pass after the change; the agent should demonstrate that at least once.

Running several agents on one Python project with AgentVera

For larger Python refactors, running multiple Cursor Agent sessions in parallel can speed things up, but plain parallel edits often collide in the same files. AgentVera gives each agent its own git worktree, so they can work on separate branches without overwriting one another’s changes. When an agent finishes, an automatic code review with file:line findings catches Python-specific issues such as type errors before they reach your branch. Checkpoints also let you undo a bad refactor in one click, which is valuable when an agent introduces a circular import deep in a package. See Best tools to run AI coding agents in parallel (2026) for the parallel workflow.

Wrap-up

Cursor Agent is most useful on Python projects when you treat it as a fast, context-aware CLI pair programmer rather than a replacement for your test suite. Keep project rules explicit, work in small verifiable steps, and always run the real Python tooling after each change. That discipline keeps the agent’s output maintainable and safe to merge.

Questions

How do I start Cursor Agent in a Python project?

Open a terminal in the project root and run cursor-agent. It reads instructions from AGENTS.md or .cursor/rules, so keep those files focused on build, test, and style commands for Python.

Can Cursor Agent use my Python virtual environment?

Yes. The agent runs commands in the terminal session where you started it. Activate the virtual environment first, or include the activation step in your project rules so the agent uses the correct interpreter.

How do I get Cursor Agent to run pytest and fix failures?

Ask it to run pytest -q, analyze the failure output, and apply the smallest fix. Then ask it to re-run the failing test plus related tests before you review the diff.

Does Cursor Agent respect pyproject.toml for linting and type checking?

It uses whatever commands you tell it to run. Put ruff check ., mypy src, and pytest in AGENTS.md, and the agent will invoke those tools with your pyproject.toml configuration.

Bring your agents to one desk.

Download AgentVera for free; your installed CLIs are ready to go.

More Cursor Agent guides

Other agents for Python