Python Code Prompt (Working and Explained)
"Write me a script" produces incomplete code; this prompt defines input, output, error handling and tests up front.
You are a Python engineer. Write a script that runs on the first try. SCRIPT - Purpose: [WHAT IT DOES] - Python version: [3.11 / 3.12] - Input: [FILE FORMAT / ARGUMENTS / API] - Output: [FILE / PRINT / DATABASE] - Libraries allowed: [STANDARD LIBRARY ONLY / REQUESTS / PANDAS] - Error behaviour: [FAIL FAST / SKIP AND LOG / RETRY] OUTPUT 1) REQUIREMENTS — the libraries and why, or "standard library only". 2) CODE — complete file, with `if __name__ == "__main__":`, argparse for inputs, and logging instead of print. 3) ERROR HANDLING — what happens on missing file, bad encoding, network failure, malformed row. 4) EXAMPLE RUN — the command line and the expected output. 5) TESTS — pytest tests for the happy path and two error cases. 6) LIMITS — what breaks at 10x the data volume and the one change that would fix it. RULES - No placeholders, no "...", no pseudo-code. - Type hints on every function. Docstring on public functions. - Do not catch bare exceptions; catch the specific ones. - Keep it under 150 lines; if it needs more, split into functions and say so.
If the prompt does not say what happens when the file is missing, the script will crash on it.
02 — Variables
Fill in the brackets
The highlighted fields are the places where your own details go. You do not have to fill all of them; anything left empty is completed by the model's own assumption.
[WHAT IT DOES]
[FILE FORMAT / ARGUMENTS / API]
[FILE / PRINT / DATABASE]
[STANDARD LIBRARY ONLY / REQUESTS / PANDAS]
[FAIL FAST / SKIP AND LOG / RETRY]
[3.11 / 3.12]
Field to fill in the prompt
03 — How to use
How to use it, step by step
- 01
State the Python version — 3.12 syntax fails on 3.8 and vice versa.
- 02
Say what should happen on error. "Fail fast" and "skip and log" produce very different scripts.
- 03
Run the example command before reading the code; if it works, the code is probably right.
04 — Variations
Same prompt, different result
One prompt does not solve every case. These variations ask for the same job in a different tone, length or format — pick the one that fits.
Pandas pipeline
Same structure for a data pipeline: read, clean, transform, write, with row counts logged at each step.
CLI tool
Same structure as a CLI: subcommands, --help output, exit codes, and a config file.
05 — Tips
Notes that take the result up a level
- Scripts fail on encoding and paths, not on logic. Specify UTF-8 and use pathlib.
- Print statements are not logging. Ask for logging and you get something you can debug later.
06 — FAQ
Frequently asked about Software and Code
Why does generated code fail on the first run?
Usually a missing import, a wrong library version, or an unstated assumption about the input format. The contract fields exist to remove the third.
Should I use an AI-written script in production?
Only after review, tests and error-path checks. Treat it as a competent first draft.
Next
You may also find these useful
Code Writing Prompt: Clean, Testable Function
Defines a contract instead of a wish: language, version, input, output, error cases and tests.
Intermediate ChatGPTClaudeDebug Prompt: From "Why Doesn't It Work" to a Diagnosis
Converts "why doesn't it work" into a systematic diagnosis: reproduce, hypothesise, isolate, verify.
Intermediate ChatGPTClaudeRefactor Prompt: Improve Without Breaking Behaviour
Simplifies working code without changing behaviour, planned as reversible steps you can review one at a time.
Advanced ChatGPTClaudeSQL Query Prompt (Schema Included)
SQL written without the schema rarely runs; this prompt requires schema, relationships and expected output.
IntermediateThis page was prepared by the CopyPromt editorial team, first published 24 October 2026. Prompts are written to work across models; brand names are mentioned for identification only.