Python uv Self-contained Python scripts with uv TLDR You can add uv into the shebang line for a Python script to make it a self-contained executable. I am working on a Go project to better learn the language. It's a simple API backed by a postgres database. When I need to test out an endpoint, I prefer to use the httpx python package inside an ipython REPL over making curl requests. It's nice to be able to introspect responses and easily package payloads with dicts instead of writing out JSON. Anyway, I decided to write a script to upsert some user data so that I can beat on my /users endpoint. My jam_users.py script looks like this: import httpx import IPython from loguru import logger users = [ dict(name="The Dude", email="the.dude@abides.com", password="thedudeabides"), dict(name="Walter Sobchak", email="walter@sobchak-security.com", password="vietnamvet"), dict(name="Donnie", email="donniesurfs@yahoo.com", password="iamthewalrus"), dict(name="Maude", email="mauddie@avant-guard.com", password="goodmanandthorough"), ] r = httpx.get("http://localhost:4000/v1/users") r.raise_for_status() for user in r.json()["users"]: logger.info(f"Deleting: {user['name']}") r = httpx.delete(f"http://localhost:4000/v1/users/{user['id']}") r.raise_for_status() for user in users: r = httpx.post("http://localhost:4000/v1/users", json=user) r.raise_for_status() logger.info(f"Created: {r.json()}") IPython.embed() This is really straight-forward. It will clear out any existing users and then insert these test users. Right after that, I get dropped into an ipython repl to do what I need for testing. All I have to do is run: However, if I want to run the script as-is, I will need to choose one of these approaches: Install the dependencies httpx, IPython, and loguru globally in my system python Create a virtual environment, activate it, install deps, and run my script while the venv is activated These are both not great options in my opinion. These approaches also rely on having a system pyt...
First seen: 2025-03-30 03:31
Last seen: 2025-03-30 21:35