Email or username:

Password:

Forgot your password?
Simon Willison

I figured out a prompting pattern for getting Claude to produce fully self-contained Python scripts that execute with "uv run" using PEP 723 inline script dependencies - and now I can one-shot useful Python utilities with it simonwillison.net/2024/Dec/19/

16 comments
Simon Willison

Here are my custom instructions which I'm using as part of a Claude Project, but I expect they'll work the same way with other LLMs too

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",
# ]
# ///
Jeff Triplett

@simon I am happy to see you are using this. I wasn't doing as much one-shot attempts with Projects when we talked at DCUS but sometimes that's what the results are.

I have been playing with MCP and it has a ton of potential but both the UI and the quality was a huge drop after two days of near perfection. Not sure if you tried that out yet, but I haven't tried it in the last two weeks. (I suspect it was an obvious bug)

Happy to compare notes some times.

Simon Willison

I have a similar set of custom instructions I use with Claude Artifacts to get it to produce mobile-friendly single page HTML apps that run without a build step

Never use React in artifactsβ€”always plain HTML and vanilla JavaScript and CSS with minimal dependencies.

CSS should be indented with two spaces and should start like this:

<style>
* {
  box-sizing: border-box;
}
Inputs and textareas should be font size 16px. Font should always prefer Helvetica.

JavaScript should be two space indents and start like this:

<script type="module">
// code in here should not be indented at the first level
Simon Willison

@evan love Web Components - I often prompt it to create those directly, but I still want them all in a single file so I can easily copy and paste the whole lot out of the LLM at once

Evan Prodromou

@simon So, you don’t use an IDE with integrated AI, like VSCode with CoPilot or Cursor or Zed or whatever?

Simon Willison

@evan I use GitHub copilot but I get a ton of work done directly in Claude (with Artifacts) and ChatGPT (with Code Interpeter) pasting code back and forth

Evan Prodromou

@simon last question! Do you ever use a local Open Suorce model for code generation, and if so, which one?

Simon Willison

@evan both of them use so much memory that I have to shut down a bunch of Firefox tabs and VS code windows first - plus they're noticeably slower than the best hosted models

Kyle Howells

@simon @evan I also only use local models on flights really. However, I will say I've found the smaller Qwen2.5-coder models on Ollama to be good enough, and importantly fast enough for most simple boiler plate work without taking up all my ram to run them.

Simon Willison

@prem_k with Claude Artifacts you have to or it defaults to writing you a React component every time!

Prem Kumar Aparanji πŸ‘ΆπŸ€–πŸ˜

@simon true that ... I say some version of "native JavaScript without libraries as much as possible"

mikeful

@simon Put Zen of Python in the prompt and see if it generates better Python code.

Tom Bortels

@simon

I'm a big fan of this style; below a certain level of complexity, single-file standalone scripts with the inline requirements - plus uv - are a game changer in being able to easily share and deploy things for non-python developer users.

Go Up