Skip to content

Instantly share code, notes, and snippets.

@dai
Last active July 22, 2025 01:35
Show Gist options
  • Select an option

  • Save dai/c34fbb69604b7d51a8987b1c9ace7c43 to your computer and use it in GitHub Desktop.

Select an option

Save dai/c34fbb69604b7d51a8987b1c9ace7c43 to your computer and use it in GitHub Desktop.
Uv: Running a script with dependencies

Uv: Running a script with dependencies comment by simonw

「スクリプトの依存関係の宣言」は非常に便利です: https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies

  # /// script
  # dependencies = [
  #   "requests<3",
  #   "rich",
  # ]
  # ///
  import requests, rich
  # ... script goes here

これをscript.pyとして保存すると、 uv run script.py を使用して、指定された依存関係とともに実行できます。依存関係についてまったく考えなくても、一時的な仮想環境に自動的にインストールされます。 これは Python PEP 723 の実装です: https://peps.python.org/pep-0723/

Claude 4 は実際にこのトリックを認識しており、インライン スクリプトの依存関係を持つ Python スクリプトを作成するように要求すると、適切な処理が実行されます。たとえば、https://claude.ai/share/1217b467-d273-40d0-9699-f6a38113f045 のプロンプトは次のとおりです。

Write a Python script with inline script dependencies that uses httpx and click to download a large file and show a progress bar.

Claude 4 より前は、これを行う方法に関する特別な手順を含むカスタム Claude プロジェクトがありましたが、これはもう必要ありません: https://simonwillison.net/2024/Dec/19/one-shot-python-tools/

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