Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Last active July 22, 2025 08:34
Show Gist options
  • Save pythoninthegrass/e5b0e23041fe33526669bb2487bd6042 to your computer and use it in GitHub Desktop.
Save pythoninthegrass/e5b0e23041fe33526669bb2487bd6042 to your computer and use it in GitHub Desktop.
Demo for uv's PEP 723 script support with various dependencies.
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx>=0.28.1",
# "python-decouple>=3.8",
# "sh>=2.2.2",
# ]
# [tool.uv]
# exclude-newer = "2025-07-21T00:00:00Z"
# ///
# pyright: reportMissingImports=false
"""
Usage:
hello <get|ls|env|greet>
Args:
get: Make a GET request to GitHub API
ls: List files in current directory
env: Show environment variable
greet: Show welcome message
Note:
Demo for uv's PEP 723 script support with various dependencies.
Dependencies get cached in `uv cache dir`
e.g., ~/Library/Caches/uv/environments-v2/hello-8969d74899f61209
"""
import httpx
import sys
from pathlib import Path
from sh import ErrorReturnCode, ls
def main():
# Use match-case to handle different commands
match sys.argv[1] if len(sys.argv) > 1 else "help":
case "get":
# Make a GET request using httpx
response = httpx.get("https://api.github.com")
print(f"Response from GitHub API: {response.status_code}")
case "ls":
# Run the ls command using sh
try:
output = ls("-l")
print(f"Output of 'ls -l':\n{output}")
except ErrorReturnCode as e:
print(f"Error running command: {e}")
case "env":
# Get an environment variable using decouple
env_file = Path.cwd() / '.env'
if env_file.exists():
from decouple import Config, RepositoryEnv
config = Config(RepositoryEnv(env_file))
my_var = config("HELLO", default="world")
else:
from decouple import config
my_var = config("HELLO", default="world")
print(f"Hello, {my_var}!")
case "greet":
# Print a welcome message
print("Welcome to the greet script!")
case _:
print(__doc__.strip())
if __name__ == "__main__":
main()
@pythoninthegrass
Copy link
Author

# download to ~/.local/bin (i.e., somewhere in PATH)
curl -o ~/.local/bin/hello https://gist.githubusercontent.com/pythoninthegrass/e5b0e23041fe33526669bb2487bd6042/raw/hello
chmod +x ~/.local/bin/hello

# run from any directory
cd ~/Desktop
hello

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment