Skip to main content

Claude/ChatGPT Prompt to Refactor a Python Script into a Package

Prompt to turn a monolithic Python script into a clean, testable package: layered modules, type hints, DI, pytest, pyproject, and a CLI entry point.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt

                                

What this prompt does

This prompt makes the model a senior Python engineer who values testability over cleverness and asks it to refactor a monolithic script into an installable package without changing its behavior, returning working code rather than pseudocode. It defines six deliverables: a module split by responsibility (input/IO, core processing, output, orchestration), type hints throughout with small pure functions where the logic allows, dependency injection for the external services you name so they can be mocked, a pytest suite with mocks covering the main path and at least two failure cases, a pyproject.toml with build config and a console_scripts entry point, and a short CHANGES note listing any subtle behavior differences. The structure works because it creates seams — the boundaries that make a previously untestable script testable.

Four variables drive it. [package_name] names the target package (default report_toolkit). [python_version] sets the language target. [external_deps] lists the services to isolate behind DI, like a database client, S3, and SMTP. [script] is where you paste the actual code to refactor. Dependency injection in deliverable three is the load-bearing part: pinning it carefully is what makes the pytest suite in deliverable four genuinely meaningful, because mockable external services let the tests exercise real logic instead of hitting live systems.

When to use it

  • You have a standalone Python script that two or more people now depend on.
  • A 300-line file has hit a ceiling and needs seams for testing and mocking.
  • You want behavior preserved exactly while structure improves.
  • External services like a database, S3, or SMTP need to be injected and mockable.
  • You want a pytest suite covering the main path and at least two failure cases.
  • You need a proper package with pyproject.toml and a console_scripts entry point.

Example output

Expect the file tree first, then each file in full, targeting [python_version]. The script is split into input/IO, core processing, output, and orchestration modules under the [package_name] package; type hints run throughout with small pure functions where possible; [external_deps] are injected so tests can mock them; a pytest suite covers the main path plus at least two failure cases; a pyproject.toml provides build config and a console_scripts entry point; and a CHANGES note lists any subtle behavior differences that could not be avoided. Behavior is preserved, so it is a faithful restructuring rather than a rewrite.

Pro tips

  • Paste the complete script into [script]; partial code forces the model to guess at the behavior it must preserve.
  • List every service in [external_deps] you want mockable, since each becomes a DI seam for tests.
  • Pin the DI carefully — it is what makes the pytest suite actually meaningful rather than testing nothing.
  • Read the CHANGES note closely; subtle behavior differences are where a "behavior-preserving" refactor can surprise you.
  • Set [python_version] to your real interpreter so type-hint syntax and packaging metadata match.
  • If the module split feels off, re-run clarifying which logic is IO versus core processing in your [script].

Frequently Asked Questions

Does this refactor change the script's behavior?
No, the prompt explicitly asks to preserve behavior while improving structure. It also requires a CHANGES note listing any subtle behavior differences that could not be avoided, so you can review anything that shifted during the restructuring.
How does it make my script testable?
It splits the code into IO, core processing, output, and orchestration modules and injects the services you list in `[external_deps]`. Dependency injection creates seams so the pytest suite can mock external systems and exercise real logic without hitting live services.
What do I need to provide?
You paste your existing code into the `[script]` variable and name the services to isolate in `[external_deps]`. Provide the complete script rather than a fragment, since partial code forces the model to guess at the behavior it must preserve.
Does the output include a packaged, installable result?
Yes. It produces a pyproject.toml with build config and a console_scripts entry point under your `[package_name]`, turning the script into a proper installable package rather than a loose file.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Python & Automation Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support