nikkie-ftnextの日記

イベントレポートや読書メモを発信

uv 0.4.0 リリースノートより、Pythonプロジェクトの扱いの変更。再配布可能にするかを--appや--libフラグで指定する

はじめに

美咲ちゃのおてて、ふわっふわ...😇 nikkieです。

先日 uv 0.3.0 を紹介しましたが、そのすぐ後に 0.4.0 が登場!

0.4.0のリリースノートより、興味を持った点を調べました。

目次

uv 0.4.0 リリースノート

This release adds first-class support for Python projects that are not designed as Python packages (e.g., web applications, data science projects, etc.).

この一文を見て「おっ!」と思ったんですよね。
0.3.x までの uv(やその前に注目を集めたRye)は、再配布可能なPythonパッケージを前提にしている傾向がありました。
ですが、先日の記事で書き出してみましたが、Pythonプロジェクトは常に再配布するとは限りません

リリースノートにあるように、Webアプリケーションやデータサイエンスプロジェクトは再配布しないケースだと思います。
なので常に再配布するという一律な対応に、私としては正直微妙かなと感じるところもありました。

ところが、uv 0.4.0 からは、再配布するプロジェクトもしないプロジェクトもuv initできるようになったのです!
これは私としては歓迎したい機能追加です。

ポイントはuv init--app--libというフラグです。

百聞は一見に如かず

uvのバージョンアップも兼ねて実施しました。

% uv --version
uv 0.3.3 (Homebrew 2024-08-23)
% uv init example
% tree -L 1 example
example
├── README.md
├── pyproject.toml
└── src

2 directories, 2 files
% uv --version
uv 0.4.7 (Homebrew 2024-09-07)
% uv init example2
% tree -L 1 example2
example2
├── README.md
├── hello.py
└── pyproject.toml

1 directory, 3 files

「Creating projects」(Projectsのドキュメントより)

uvの概念(Concept)をまとめたドキュメントのProjectの説明が分かりやすかったです。
https://docs.astral.sh/uv/concepts/projects/#creating-projects

When creating projects, uv distinguishes between two types: applications and libraries.

By default, uv will create a project for an application. The --lib flag can be used to create a project for a library instead.

  • デフォルトではアプリケーション向けのプロジェクト
  • --libフラグを指定すると、代わりにライブラリ向けのプロジェクト

ドキュメントのこのセクションは、uv initが作るファイル全てに触れていて、情報量満載で分かりやすいので、一読がオススメです。

アプリケーション(--app

以下の2つで作れます

  • uv init(フラグの指定なし)
  • uv init --app

A build system is not defined and the source code is in the top-level directory, e.g., hello.py.

pyproject.tomlには[build-system]が定義されず、ソースコードはトップレベルにhello.pyと配置されます。

The project does not contain a package that will be built and installed into the project environment.

✍️アプリケーションのプロジェクトは、パッケージを含まない

ライブラリ(--lib

A library is a project that is intended to be built and distributed as a Python package (略)

他のプロジェクトに使われる関数やオブジェクトを提供するのが、ライブラリ。

When creating a library, uv defines a build system and places the source code in placed in a src directory.

pyproject.tomlには[build-system]が定義され、ソースコードsrcディレクトリの下に配置されます。
この配置はsrc-layoutと呼ばれますね

--app --package

再配布したいアプリケーション(distributable application)の場合に指定。

e.g., if you want to publish a command-line interface via PyPI.

pyproject.tomlには[build-system][project.scripts]が定義され、ソースコードsrcディレクトリの下に配置されます。

「Build systems」も確認

https://docs.astral.sh/uv/concepts/projects/#build-systems

uv uses the presence of a build system to determine if a project contains a package that should be installed in the project virtual environment.

  • pyproject.tomlに[build-system]が定義されていないとき、プロジェクト自体をビルドしたりインストールしたりしようとしない
    • uv init --appのときだ!
  • [build-system]が定義されているとき、プロジェクトをプロジェクトの環境にビルドしたりインストールしたりする
    • uv init --libuv init --app --package

また[tool.uv.package](true / false を指定)についても言及されています。
この設定項目はPoetryみたいですね

uv init のドキュメント

https://docs.astral.sh/uv/reference/cli/#uv-init

--app

By default, an application is not intended to be built and distributed as a Python package.

--lib

A library is a project that is intended to be built and distributed as a Python package.

興味を引いたオプションのメモ

  • --no-readme
    • Do not create a README.md file

    • pyproject.tomlのREADME.mdの記載もなくなると思ってます
  • --no-workspace
    • By default, uv searches for workspaces in the current directory or any parent directory.

    • TODO:workspaceという概念を理解したい
    • If a pyproject.toml is found in any of the parent directories of the target path, the project will be added as a workspace member of the parent.

終わりに

uv 0.4.0でいい感じになった感のあるuv initを見てきました。

  • 再配布したいPythonコードを書いているかどうかユーザは認識して、--app--libとフラグを指定する
  • uvの動きは(フラグによって有無が変わる)pyproject.toml[build-system]を見て切り替えている

人によってはeasyでないと感じるかもしれませんが、私としては複数のプロジェクトのパターンがあるPythonなら明示したほうがよいと思うので、かなり好ましいです。
細かなアップデート、ありがとうございます!