Claude Codeのスキルで外部ライブラリを使いたい。しかし仮想環境は管理したくない。
uv shebang を使えば、1ファイルで完結する。
uv shebangとは
shebang行に uv run --with を指定すると、uvが依存関係を自動解決してスクリプトを実行する。
#!/usr/bin/env -S uv run --with httpx
スキルでの使い方
.claude/skills/{skill-name}/
├── SKILL.md
└── scripts/
└── example.py # 実行権限を付与
chmod +x scripts/example.py
実例:GitHub Star Counter
github_stars.py
#!/usr/bin/env -S uv run --with httpx
import sys
import httpx
repo = sys.argv[1]
stars = httpx.get(f"https://api.github.com/repos/{repo}").json()["stargazers_count"]
print(f"{repo}: {stars:,} stars")
実例:Qiita Tag Info
記事のタグ候補提案に使用。Qiita記事執筆支援スキルの一部。
qiita_tag_info.py
#!/usr/bin/env -S uv run --with httpx
import sys
import httpx
tag = sys.argv[1]
data = httpx.get(f"https://qiita.com/api/v2/tags/{tag}").json()
print(f"{data['id']}\t{data['items_count']}\t{data['followers_count']}")
まとめ
- shebang1行で外部ライブラリが使える
- 仮想環境不要
- uvさえあればどこでも動く