How to Use ChatGPT to Generate and Debug Python Code

Short answer: Tell ChatGPT the goal, the inputs and outputs, your Python version and any limits. Run the code it gives you. If it fails, paste the full error with the code, ask for the cause and a fix, and test again. Never run code you do not understand on important data.
What ChatGPT does well with Python
- Writing small scripts: renaming files, cleaning a CSV, calling an API, sending a report.
- Explaining code in plain language.
- Finding the cause of an error message.
- Suggesting tests and cleaner structure.
- Converting between formats or languages.
It can still write code that looks right and is wrong, uses a library incorrectly or invents a function that does not exist. Run and test everything.
How to write a good coding prompt

Include five parts:
- 1Goal: what the program should do, in one or two sentences.
- 2Inputs and outputs: file names, columns, formats, an example row.
- 3Language and version: for example Python 3.12 and the libraries you have installed.
- 4Constraints: no external libraries, must run on Windows, must handle empty files.
- 5Example: a small sample of input and the output you expect.
Example prompt: Write a Python 3.12 script that reads sales.csv with columns date, region and amount, and prints total amount per region for the current month. Use only the standard library. Handle missing values by skipping the row and counting skipped rows. Sample row: 2026-12-03,North,1200.50. Explain how to run it.
The sample row removes a lot of guessing.
Set up so you can run the code
You need Python installed and a place to run it. Beginners can use the official installer from python.org, a code editor and the terminal. Ask ChatGPT: "I am on Windows (or Mac). Explain step by step how to install Python and run a script called sales.py." Follow the official Python documentation for installation steps, since versions and screens change.
Work in a test folder with copies of your files, not the originals.
The debugging loop

- 1Run the code.
- 2Copy the full error, including the line numbers, not only the last line.
- 3Ask with context: paste the code, the error, what you expected and what happened.
- 4Apply the fix.
- 5Test again.
Prompt: This Python script fails. Explain in plain English what the error means, which line causes it and why, then show the corrected code. Code: [paste]. Error: [paste]. I expected [result].
If the fix does not work, say what changed. Do not just write "still broken". Include the new error.
Ask it to explain
Understanding the code protects you. After you get a script, ask:
- "Explain this code line by line for a beginner."
- "What could go wrong with this script?"
- "What happens if the file is empty or the column is missing?"
If you cannot explain a part, do not rely on it.
Ask it to write tests
Tests check that code works and keep working when you change it.
Prompt: Write pytest tests for this function. Include normal cases, empty input, bad input and one edge case. Explain what each test checks. Function: [paste]
Run the tests. A passing test suite is better evidence than a confident explanation.
Improve and clean the code
Once it works, ask for a review.
Prompt: Review this code for readability, errors and performance. List the three most important changes and show the improved version. Keep behaviour the same. Code: [paste]
Compare the result with the original and run your tests again.
Prompts to copy
Clean a CSV: Write a Python script using pandas that loads data.csv, trims spaces in text columns, removes duplicate rows, converts the date column to dates and saves cleaned.csv. Print how many rows were removed.
Call an API safely: Write a function that calls this API endpoint with the requests library, handles timeouts and errors, and returns the JSON. Read the API key from an environment variable. Never hard-code the key.
Automate a report: Write a script that reads a spreadsheet, builds a summary by month and saves it to a new Excel file.
Explain an error: Here is my traceback: [paste]. List the three most likely causes in order and how to check each.
Safety rules
- Never paste secrets. API keys, passwords and tokens do not belong in a prompt or in code you share. Use environment variables.
- Do not run code you do not understand on real data, servers or files you cannot restore.
- Work on copies and use version control if you can, so you can undo changes.
- Check libraries. Make sure packages it suggests exist and are well known before installing them. Check the name carefully, since misspelled package names can be malicious.
- Mind licences and company rules. Check your employer's policy on AI-generated code and on pasting company code into AI tools.
- Handle personal data properly. Do not paste customer data into prompts.
Limits to remember
ChatGPT does not run your environment. It does not know your exact library versions unless you tell it. It may use old syntax or functions that were removed. When you see a "no attribute" or "module not found" error, give it your version numbers.
Common mistakes
- Asking for a large program in one go. Build in small pieces.
- Not sharing the full error.
- Accepting code without running it.
- Forgetting to say the Python version and libraries.
- Pasting private data or keys.
FAQ
Can ChatGPT write Python code?
Yes. It writes working code for many tasks. You must run and test it.
Can I learn Python with ChatGPT?
It is a useful tutor if you ask it to explain, give you exercises and review your attempts. Practise by writing code yourself.
Why does the code ChatGPT gave me not work?
Common causes are version differences, missing libraries, wrong file paths or a misunderstanding of your data. Paste the full error and your setup.
Is it safe to run code from ChatGPT?
Only after you understand it, on copies of your data. Do not run anything that deletes files or touches systems you cannot restore.
Can ChatGPT debug someone else's code?
Yes, if you paste the code and the error. Check with your organisation before pasting company code.
Key takeaways
- A precise prompt with a sample row beats a vague request.
- Use the debugging loop: run, copy the full error, ask, fix, test.
- Ask for explanations and tests.
- Protect secrets and work on copies.
Want custom automations built by a team that tests them properly? Book a free 30 minute call with Tech AI Labs.
FAQ
Quick answers
Can ChatGPT write Python code?
Yes. It writes working code for many tasks. You must run and test it.
Can I learn Python with ChatGPT?
It is a useful tutor if you ask it to explain, give you exercises and review your attempts. Practise by writing code yourself.
Why does the code ChatGPT gave me not work?
Common causes are version differences, missing libraries, wrong file paths or a misunderstanding of your data. Paste the full error and your setup.
Is it safe to run code from ChatGPT?
Only after you understand it, on copies of your data. Do not run anything that deletes files or touches systems you cannot restore.
Can ChatGPT debug someone else's code?
Yes, if you paste the code and the error. Check with your organisation before pasting company code.
IN THE DIRECTORY
See how this tool compares, what it costs and what it is best for.
Open the tool page →

