Source URL: https://simonwillison.net/2024/Dec/19/one-shot-python-tools/#atom-everything
Source: Simon Willison’s Weblog
Title: Building Python tools with a one-shot prompt using uv run and Claude Projects
Feedly Summary: I’ve written a lot about how I’ve been using Claude to build one-shot HTML+JavaScript applications via Claude Artifacts. I recently started using a similar pattern to create one-shot Python utilities, using a custom Claude Project combined with the dependency management capabilities of uv.
(In LLM jargon a “one-shot" prompt is a prompt that produces the complete desired result on the first attempt.)
I’ll start with an example of a tool I built that way.
I had another round of battle with Amazon S3 today trying to figure out why a file in one of my buckets couldn’t be accessed via a public URL.
Out of frustration I prompted Claude with a variant of the following (full transcript here):
I can’t access the file at EXAMPLE_S3_URL. Write me a Python CLI tool using Click and boto3 which takes a URL of that form and then uses EVERY single boto3 trick in the book to try and debug why the file is returning a 404
It wrote me this script, which gave me exactly what I needed. I ran it like this:
uv run debug_s3_access.py \
https://test-public-bucket-simonw.s3.us-east-1.amazonaws.com/0f550b7b28264d7ea2b3d360e3381a95.jpg
You can see the text output here.
Inline dependencies and uv run
Crucially, I didn’t have to take any extra steps to install any of the dependencies that the script needed. That’s because the script starts with this magic comment:
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "click",
# "boto3",
# "urllib3",
# "rich",
# ]
# ///
This is an example of inline script dependencies, a feature described in PEP 723 and implemented by uv run. Running the script causes uv to create a temporary virtual environment with those dependencies installed, a process that takes just a few milliseconds once the uv cache has been populated.
This even works if the script is specified by a URL! Anyone with uv installed can run the following command (provided you trust me not to have replaced the script with something malicious) to debug one of their own S3 buckets:
uv run http://tools.simonwillison.net/python/debug_s3_access.py \
https://test-public-bucket-simonw.s3.us-east-1.amazonaws.com/0f550b7b28264d7ea2b3d360e3381a95.jpg
Writing these with the help of a Claude Project
The reason I can one-shot scripts like this now is that I’ve set up a Claude Project called "Python app". Projects can have custom instructions, and I used those to "teach" Claude how to take advantage of inline script dependencies:
You write Python tools as single files. They always start with this comment:
# /// script
# requires-python = ">=3.12"
# ///
These files can include dependencies on libraries such as Click. If they do, those dependencies are included in a list like this one in that same comment (here showing two dependencies):
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "click",
# "sqlite-utils",
# ]
# ///
That’s everything Claude needs to reliably knock out full-featured Python tools as single scripts which can be run directly using whatever dependencies Claude chose to include.
I didn’t suggest that Claude use rich for the debug_s3_access.py script earlier but it decided to use it anyway!
I’ve only recently started experimenting with this pattern but it seems to work really well. Here’s another example – my prompt was:
Starlette web app that provides an API where you pass in ?url= and it strips all HTML tags and returns just the text, using beautifulsoup
Here’s the chat transcript and the raw code it produced. You can run that server directly on your machine (it uses port 8000) like this:
uv run https://gist.githubusercontent.com/simonw/08957a1490ebde1ea38b4a8374989cf8/raw/143ee24dc65ca109b094b72e8b8c494369e763d6/strip_html.py
Then visit http://127.0.0.1:8000/?url=https://simonwillison.net/ to see it in action.
Tags: aws, python, s3, ai, generative-ai, llms, ai-assisted-programming, claude, uv
AI Summary and Description: Yes
Summary: The text describes a practical application of AI, specifically using Claude for building Python utilities and debugging Amazon S3 access. It introduces novel techniques for dependency management through inline scripting, showcasing how AI can assist in software development, particularly for infrastructure-related tasks.
Detailed Description: The text revolves around the author’s experience using Claude, an AI assistant, to create one-shot Python utilities, specifically for debugging files on Amazon S3. The author employs Claude to generate scripts that handle various tasks without the hassle of manual dependency installation. Key insights include:
– **Use of Claude**: The author utilizes Claude’s capabilities to develop Python applications quickly. This exemplifies the growing trend of AI-assisted programming, where developers can leverage AI for efficiency.
– **One-Shot Prompts**: The term “one-shot” refers to prompts that yield complete results in a single attempt, showcasing the efficiency and capability of AI in generating complex code directly from user prompts.
– **Debugging S3 Access**: A practical example is provided where the author instructs Claude to write a debugging tool for resolving access issues in Amazon S3, illustrating how AI can solve specific infrastructure-related problems.
– **Inline Script Dependencies**: The discussion of PEP 723 highlights an innovative feature that allows developers to declare dependencies within scripts. This approach enables quick creation of virtual environments, promoting faster development cycles.
– **Custom Claude Project**: By creating a dedicated Claude Project, the author can streamline script generation tailored to specific needs, enhancing productivity in development.
– **Practical Examples**: The text shares detailed commands and examples for running the AI-generated scripts, demonstrating the practical application of these AI capabilities; one of the scripts strips HTML tags from URLs, showcasing functionality relevant to data processing.
– **Potential Security Concerns**: Although not directly addressed in the text, inviting users to run scripts from external URLs raises potential security concerns regarding trust and the risk of executing malicious code.
In summary, the text illustrates a practical integration of AI in software development, emphasizing the use of AI tools for efficiency in managing dependencies and resolving issues in cloud infrastructure, as well as prompting reflection on associated security practices.